【问题标题】:how to select a text file from a directory and read it in GUI matlab如何从目录中选择一个文本文件并在 GUI matlab 中读取它
【发布时间】:2012-12-22 08:57:19
【问题描述】:

我有一个 GUI 系统,它应该从目录中选择一个文本文件,打开它并在代码中读取它。我有这个代码(获取文本文件的部分):

function FindCallback(source,eventdata)
     [FileName,PathName]= uigetfile('*.txt','browse')
 s={};
           fid = fopen('*.txt'); 
    tline = fgetl(fid); 
        while ischar(tline) 
           s=[s;tline]; 
           tline = fgetl(fid); 
        end

它显示了一个错误:

Invalid file identifier.  Use fopen to generate a valid file
identifier.
Error in ==> GUI_oo>FindCallback at 77
    tline = fgetl(fid);
??? Error while evaluating uicontrol Callback

任何建议..

谢谢

【问题讨论】:

    标签: matlab user-interface


    【解决方案1】:

    这是你的问题:

    fid = fopen('*.txt');
    

    没有文件名“*.txt”,fopen 的参数应该是你在上面的 FileName 中存储的真实文件名。

    所以应该是这样的:

    fid = fopen([PathName FileName]);
    

    您可能还想在 fopen 之前检查 FileName 是否为 0 (IIRC),因为用户可能会取消文件选择。试试这个:

    if FileName == 0
       % maybe display a warning here
       return
    end
    

    【讨论】:

      【解决方案2】:

      应该改成

      path = fullfile([FileName PathName])
      fid = fopen(path);
      

      打开*.txt是错误的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-31
        • 1970-01-01
        • 2019-05-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多