Save imported vector array with matlab file for later use
1 view (last 30 days)
Show older comments
I have imported a text file, containing columns of numbers, as vectors.
However, when I save my matlab file, close and reopen, the data is gone. I have to reimport the data.
is there a way for the imported data/vectors in the workspace to be preserved.
0 Comments
Accepted Answer
dpb
on 6 Sep 2024
Moved: dpb
on 6 Sep 2024
When you close MATLAB itself??? No, when you close any application its working data are gone because the memory is released back to the system. MATLAB will reopen files in the editor, but it doesn't restore the whole working environment; if you want to do that routinely, you could save your workplace and then have a line in your startup.m file that reloads a given .mat file;, say "WORKSPACE".
I actually have a pair of routines that does that
% BACKUP_WORKSPACE
% Backup current workspace to "WORKING ddMMMyyyy hhmm.mat"
fn="WORKING-"+string(datetime(now,'ConvertFrom','datenum','Format','yyyy_MM_dd(MMM)_HHmm'));
if exist('comment','var')
fn=fn+"-"+string(comment);
end
clear d % don't save the dir() struct
save(fn)
clear comment % don't leave last comment hanging around -- may not like this
and
% LOADLAST.m
% load last WORKING backup file w/o needing date...
dd=dir('WORK*.mat');
[~,ix]=sort([dd.datenum],'descend');
dd=dd(ix);
load(dd(1).name)
disp(['Loaded: ' dd(1).name])
clear dd
These are scripts so they run in the workspace; the BACKUP routine will add as an addition to the file name the content of a variable comment; the LOADLAST routine looks for the latest of the set and loads it; one can get/load any specific one by manually loading the specific file.
Everything will be as you left it at the time you did the particular backup.
0 Comments
More Answers (0)
See Also
Categories
Find more on Workspace Variables and MAT-Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!