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

조회 수: 1 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by