How to load data files from different folder using for loop?

7 ビュー (過去 30 日間)
Robert  Flores
Robert Flores 2019 年 9 月 9 日
編集済み: Stephen23 2019 年 9 月 9 日
Hello,
I am trying to load all my data into two structs using a for loop. Below is a copy of my code so far, the issue is I am recieiving is a warning followed by an error. The warning is, 'Warning: Escaped character '\ ' is not valid. See 'doc sprintf' for supported special characters.', and the error is, 'Error using load Unable to read file 'Z:\RobertFlores\MATLAB'. Input cannot be a directory.'.
I think the reason why I am getting a warning is that the program is trying to find a command for \B, like \n is new line. However, I do not know if just addeing a space in between the forward slashes would fix the warning or not.
The error, I have no idea to fix. I am using a code similar to the one in this link: https://www.mathworks.com/matlabcentral/answers/178647-how-to-load-different-data-files-in-a-for-loop
If anyone can help me resolve my issue, it will be greatly appreciated, thank you.
Sincerely,
Robert Flores
CODE:
clc, clear, close all
% Data of Interest
for i = [12:19,31,34,41-93]
filename1 = sprintf('Z:\ BARB\ TestData\ ShockData_July2018\ Time_Histories_R2\ test0%d_filt_time.mat',i)
filename2 = sprintf('Z:\ BARB\ TestData\ ShockData_July2018\ Time_Histories_R2\ test0%d_raw_time.mat',i)
S1{i} = load(filename1)
S2{i} = load(filename2)
end
  1 件のコメント
Stephen23
Stephen23 2019 年 9 月 9 日
編集済み: Stephen23 2019 年 9 月 9 日
Simpler than doubling/escaping the backslashes is to provide the path as an input argument, rather than defining it as the format string:
P = 'Z:\BARB\TestData\ShockData_July2018\Time_Histories_R2\';
F = sprintf('%stest0%d_filt_time.mat',P,i)
Even better is to use fullfile:
F = fullfile(P,sprintf('test0%d_filt_time.mat',i))

サインインしてコメントする。

採用された回答

madhan ravi
madhan ravi 2019 年 9 月 9 日
Use \\ instead of \

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by