How to save matlab workplace variables by including original file names

Hi I would like to know how I can save the matlab output files (i.e. matlab workplace variables), by including original file name.
e.g. I open a file (filename.matlab) with (load filename.matlab). Then I run a code to do calculation and I get some workplace variables (e.g. flow, pressure). I want to save those variables as filename_flow.mat and filename_pressure.mat.
I will use the same code on different filename, so I would like to know how I can save my variables as mentioned above (i.e. including the originalfile name)?

 Réponse acceptée

Not sure what you're asking, but I'll take a guess.
You can save both to a single file called "yourMfilename.mat", where yourMfilename is whatever your filename is. For example, if your m-file is called test.m, then you can get test.mat in your code like this using mfilename():
fullMatFileName = sprintf('%s.mat', mfilename('fullpath')); % Get base name from m-file name.
save(fullMatFileName , 'flow', 'pressure');
Or you can save each variable to its own .mat file if you want:
fullMatFileName = sprintf('%s_flow.mat', mfilename('fullpath')); % Get base name from m-file name.
save(fullMatFileName , 'flow');
fullMatFileName = sprintf('%s_pressure.mat', mfilename('fullpath')); % Get base name from m-file name.
save(fullMatFileName, 'pressure');

3 commentaires

Hi thank you very much for your answer. But it is a little bit different from what I want. Actually, I should have mentioned more specifically in my question, sorry for confusion. Ok, below should be more clear.
I have series of matlab data files. (e.g. a.mat, b.mat, c.mat, d.mat...etc).
And I have a matlab code (e.g. code.m).
So, I load one of my data files
load a.mat
Then I run code.m on a.mat data file for calculation. Then I get output variables of "pressure" and "flow".
When I use your script mentioned above, the output variables are saved as below:
code_flow.mat code_pressure.mat
But what I would like to see is a_flow.mat a_pressure.mat
So,when I run the same code.m on b.mat, I will get b_flow.mat and b_pressure.mat
Is it possible to save in that way. Thank you very much for your time.
Try this:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.mat');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
% Get the name of the input .mat file.
fullInputMatFileName = fullfile(folder, baseFileName)
% load(fullInputMatFileName);
% Get it without the extension
[~, baseNameNoExt, ~] = fileparts(baseFileName)
% Get the name of the two output .mat files.
baseFileName = sprintf('%s_flow.mat', baseNameNoExt);
fullOutputMatFileName1 = fullfile(folder, baseFileName)
baseFileName = sprintf('%s_pressure.mat', baseNameNoExt);
fullOutputMatFileName2 = fullfile(folder, baseFileName)
Then, with those two filenames, call save() twice like I already showed you.
save(fullOutputMatFileName1, 'flow');
save(fullOutputMatFileName2, 'pressure');
wow that works totally!! Thanks a lot.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Share and Distribute Software dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by