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

7 vues (au cours des 30 derniers jours)
Robert  Flores
Robert Flores le 9 Sep 2019
Modifié(e) : Stephen23 le 9 Sep 2019
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 commentaire
Stephen23
Stephen23 le 9 Sep 2019
Modifié(e) : Stephen23 le 9 Sep 2019
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))

Connectez-vous pour commenter.

Réponse acceptée

madhan ravi
madhan ravi le 9 Sep 2019
Use \\ instead of \

Plus de réponses (0)

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by