MATLAB Answers

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

57 views (last 30 days)
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:
If anyone can help me resolve my issue, it will be greatly appreciated, thank you.
Robert Flores
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)
  1 Comment
Stephen Cobeldick
Stephen Cobeldick on 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))

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by