How to use eval() command to save the workspace variables in different working directory?

18 ビュー (過去 30 日間)
The following code works fine in my GUI
Workspace variable : i=1; simtype='gopi'; t=[0 1]; e='hi';
eval(['save Batch_' num2str(i) '_Results_' simtype '.mat t;']); % storing of 't' variable in pwd
eval(['save Batch_' num2str(i) '_Results_' simtype '.mat ' e ' -append;']); % storing of some variables in pwd
eval (['Rin=load(''Batch_' num2str(i) '_Results_PE15.mat'');']); % load again the same variables from pwd
The above code used to save my workspace variables in present working directory. But i would like to save those workspace variables in different working directory. I have tried some comments but it doesnot work good, it shows error. Please check those codes below:
Error comes while using following codes:
%% pwds - different working directory - D:\matlabfiles\ACTIVITY\Itr 3\Script
eval(['save(fullfile(' pwds 'Batch_' num2str(i) '_Results_' simtype '.mat,t;']);
I am getting follwing error only when i am trying to save the file in different working directory (used above code).
% Error: Invalid use of operator
Kindly help me to get the correct code format for saving the workspace variables(not all variables - only some variables) in different working directory which also works in loops, if possible write through eval command itself.
  1 件のコメント
Stephen23
Stephen23 2021 年 2 月 9 日
編集済み: Stephen23 2021 年 2 月 9 日
"How to use eval() command to save the workspace variables in different working directory?"
Your task can be resolved much easier and much more efficiently using basic function syntax (just like the save documentation states) instead of command syntax that you used:
Your inefficient, indirect approach using eval:
eval(['save Batch_' num2str(i) '_Results_' simtype '.mat t;']);
The simpler and more efficient approach:
save(['Batch_',num2str(i),'_Results_',simtype,'.mat'],'t')
Or even better with sprintf:
fnm = sprintf('Batch_%d_Results_%s.mat',i,simtype);
save(fnm,'t')
Now is a good time to learn the difference.

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

採用された回答

Image Analyst
Image Analyst 2021 年 2 月 9 日
Do not use eval. It is not needed - it's virtually never needed. Just use the commands themselves without wrapping them inside eval():
% Use a different folder ("D:\matlabfiles\ACTIVITY\Itr 3\Script") than the current working directory.
folder = 'D:\matlabfiles\ACTIVITY\Itr 3\Script' % Specify folder.
baseFileName = sprintf('Batch_%d_Results_%s.mat', i, simtype); % Specify the base file name.
% Construct the full file name.
fullFileName = fullfile(folder, baseFileName);
% Save to a .mat file.
save(fullFileName, 't');
  2 件のコメント
Gopinath Karuppannan
Gopinath Karuppannan 2021 年 2 月 9 日
Hi
Thanks for your quick response.
And it works good. i can able to save those files.
And just for understanding: Can we able to use eval() command to achieve this same working? - as workspace files to be stored in different working directory.
Thanks in advance
Stephen23
Stephen23 2021 年 2 月 9 日
編集済み: Stephen23 2021 年 2 月 9 日
"Can we able to use eval() command to achieve this same working? - as workspace files to be stored in different working directory."
Why do you need eval to do that?
Every MATLAB user can simply write files to any directory by using absolute/relative filenames and the function form of some file exporting command. So far you have not given any reason why that would not work for you.

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

その他の回答 (1 件)

randerss simil
randerss simil 2021 年 2 月 9 日
%% pwds - different working directory - D:\matlabfiles\ACTIVITY\Itr 3\Script
eval(['save(fullfile(' pwds 'Batch_' num2str(i) '_Results_' simtype '.mat t' ')' ')']);
Requires closing parenthesis ... try above
  6 件のコメント
randerss simil
randerss simil 2021 年 2 月 9 日
You shud follow image analyst answer. Read the tips section of the eval function in below link
Gopinath Karuppannan
Gopinath Karuppannan 2021 年 2 月 9 日
I am getting error as
% Error: Invalid use of operator
Okay thanks, I will use as per image analyst answer.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by