How to save a mixed data (strings and integers) from workspace into a matlab data file?
29 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I want to save information processed from a large group of data into a matlab data file. For example:
a = 1253, b = 5869, c = 3569
char1 = "gyiuh"
column1 column2
value1 value1
value2 value2
value3 value3
.
.
.
N N
The savefile has both individual variables and tabulated data too. How can such a combination be saved in a single file so that they can be later retrieved for further processing? Thank you.
0 commentaires
Réponses (3)
Mathieu NOE
le 1 Oct 2025 à 16:11
hello
To save all variables in the current workspace to a .mat file:
save('filename.mat')
You can load the saved workspace later using the load command:
load('filename.mat')
4 commentaires
dpb
le 1 Oct 2025 à 20:02
No. While very powerful and extremely useful to hold variables of different types in a single object, the table similar as other variables must be regular in height of each variable and all elements of each variable must be defined (even if "defined" is the missing value).
The only ways to put a mish-mash of stuff together in a single variable is either as fields of a stuct or as elements in a cell array or array of cells. But, even a cell array itself, while each cell can be different in type or size, must be a regular array; MATLAB simply does not have the concept of "jagged" arrays.
dpb
le 1 Oct 2025 à 20:27
In the following
char1 = "gyiuh"
column1 column2
value1 value1
value2 value2
value3 value3
the existence what appears may be sequentially-numbered variables is indicative of not using the power of MATLAB as designed. If you do have such variables, particularly if they are numeric, it would be much better to use arrays where the above could be at 2D array column or as shown before a table of two columns. Eitheer way allows writing generic code to address the individual data whereas otherwise one has to write explicit code referrng to each specifically by name or revert to complex, hard-to-debug obfuscated coding practices that are to be avoided.
Walter Roberson
le 1 Oct 2025 à 20:46
Consider creating a struct of data to be represented, and then using jsonencode to create a text representation, and then writelines to write the text to a file.
0 commentaires
dpb
le 1 Oct 2025 à 21:33
Modifié(e) : dpb
le 2 Oct 2025 à 16:09
As example, I have a set of scripts the most basic two of which are
BACKUP_WORKSPACE.m
% 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 comment % don't save the dir() struct or comment
save(fn)
LOADLAST.m
% load last WORKING backup file w/o needing date...
dd=dir('WORKING*.mat');
[~,ix]=max([dd.datenum]);
load(dd(ix).name)
disp(['Loaded: ' dd(ix).name])
clear dd
Which SAVEs a .mat file with the present date/time in the filename as well as an optional comment string for ID purposes and the second that will retrieve the most recent of these sets for picking up after the previous save state. This does, in fact, deliberately overwrite local variables so it will reproduce those in the workspace as they were at the time. (Note it also does NOT clear existing workspace first as is such a penchant one sees in instructor-led code so new data/variables aren't destroyed in order to continue on without having to recreate new work done since.)
I have a couple of others that will find a given date or comment and one that lets select from a dropdown list as well...the possibiliities are limited only by your imagination.
0 commentaires
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!