【问题标题】:Matlab-how to give path to text files in a folderMatlab-如何给出文件夹中文本文件的路径
【发布时间】:2013-05-02 15:09:38
【问题描述】:

我在一个名为“text_files”的文件夹中有很多名为 file1、file2、file3、.... 的文本文件。当我在 Matlab 目录中手动打开该文件夹并执行以下操作时,它工作正常。

      textFiles = dir('*.txt');
  for k = 1:length(textFiles);
      filename = textFiles(k).name;
      data = fopen(filename,'r');
      datamatrix=textscan(data, '%f%f','CollectOutput',1);
      data1 = datamatrix{:,1};
      r=data1(:,1);v0=data1(:,2);
      figure(k);
      ph=plot(r,v0);
      xlabel('a'); 
      ylabel('b');

      temp=['fig',num2str(k),'.eps'];
      print(gcf,'-depsc',temp);
      fclose(data);
  end

我的 Mac 中文本文件的路径是 '/Users/ram/group1/sales/text_files'。我想要做的不是手动打开 matlab 目录中的文件夹,而是编写一个自动为我完成的脚本。所以,我想我必须在

    textFiles = dir('*.txt');

任何帮助将不胜感激。

【问题讨论】:

    标签: matlab path directory


    【解决方案1】:

    使用完整路径:

    src_dir = '/Users/ram/group1/sales/text_files';
    textFiles = dir( fullfile( src_dir, '*.txt' ) );
    for k = 1:numel(textFiles)
         filename = fullfile( src_dir, textFiles(k).name ); % NOTE the use of src_dir here as well!
         % continue as usuall...
    

    【讨论】:

      猜你喜欢
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多