Save imported vector array with matlab file for later use

1 vue (au cours des 30 derniers jours)
Bill White
Bill White le 6 Sep 2024
Déplacé(e) : dpb le 6 Sep 2024
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.

Réponse acceptée

dpb
dpb le 6 Sep 2024
Déplacé(e) : dpb le 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.

Plus de réponses (0)

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by