Retrieving variables from multiple workspaces and use them in for loop

I've generated (many) folders which contain each a workspace (named wksp) and some figures. The name of the folders are generated automatically and depend on parameter values, alpha and beta, given in my program. Ex:
mkdir(sprintf('/Users/OUTPUTalpha%gbeta%g',alpha,beta)).
Suppose I've three numerical values for alpha, while beta remains unchanged (and is equal to betavalue). How do I retrieve variables from workspace (wksp) in the different folders with the help of a for-loop, and plot these variables into the same figure?
What I try is something like this:
alphavalues = [1 20 300];
Path(1,:) = '/Users/OUTPUTalpha1gbetavalue ';
Path(2,:) = '/Users/OUTPUTalpha20gbetavalue ';
Path(1,:) = '/Users/OUTPUTalpha300gbetavalue';
figure;
for i = 1:length(alphavalues)
load fullfile('Path(i,:)','wksp.mat')
plot(variable)
hold on;
end
hold off;
I get an error message saying that the file sprintf(path(i,:)) does not exist.
While the pathname shows nicely when typing
sprintf(Path(1,:)), for example, in the command window,
it only returns
ans = sprintf(Path(1,:))/wksp.mat when using fullfile.
Thank you for your help.

Réponses (1)

thisdata = load( fullfile( Path(i,:), 'wksp.mat') );
plot(thisdata.variable)

1 commentaire

Thanks a lot, Walter! Thanks to your code, I realized that my problem consisted also in not removing potential empty space at the end of Path(i,:). Using strtrim(Path(i,:)) it works just fine.

Connectez-vous pour commenter.

Question posée :

le 9 Juin 2017

Commenté :

le 11 Juin 2017

Community Treasure Hunt

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

Start Hunting!

Translated by