[R2017a] save nested structure with save, append files, load it and check some 'string' fields with strcmp

2 vues (au cours des 30 derniers jours)
Hey guys,
I am working on a GUI that allows users to save the data, initially in a text file, inside a nested structure into a file .m
clc; clear;
s1(1).name = 'CaCO3.....';
s1(1).time = [0 1 2 3 4 5 6 7 8 9 10];
s1(1).mass = [0.9,0.9,0.8,0.7,0.7,0.5,0.4,0.4,0.4,0.,4];
s1(1).heat = [0 1 2 3 4 5 6 7 8 9 10];
s1(2).name = 'MgCO3.....';
s1(2).time = [0 2 4 6 8 10 12 14 16 18];
s1(2).mass = [1 1 1 1 1 1 1 1 1 1];
s1(2).heat = [9 9 9 9 9 9 9 9 9 9];
save('newstruct.mat', 's1');
disp('Contents of newstruct.mat:')
whos('-file', 'newstruct.mat')
clear('s1')
load('newstruct.mat')
A_cell = struct2cell(s1);
I want now to access all the field s1.name and check if among those there is a particular string, let's say 'CaCO3.....'.
Both:
[s1.name]
[A_cell{1,1,:}]
give me the following result: 'CaCO3.....MgCO3.....'
my questions are:
1) How can I put all results in an array? Or if I want to loop, how can I know the total length of the field .name?
2) If the name doesn't exist, how do I create a new substrcture s1(3)?

Réponse acceptée

Walter Roberson
Walter Roberson le 22 Avr 2018
lookfor = 'CaCO3';
names = {s1.name};
[~, idx] = ismember(lookfor, names);
if idx == 0
idx = length(s1)+1;
s1(idx).name = lookfor;
end
s1(idx).time = new time information
s1(idx).mass = new mass information
s1(idx).heat = new heat information
  3 commentaires
Stephen23
Stephen23 le 23 Avr 2018
Modifié(e) : Stephen23 le 23 Avr 2018

"how can I delete s1(2)? "

Just like with any other array:

   s1(2) = []

"Will s1(3) become the new s1(2)?"

Yes, just like with any other type of array. You can try it yourself:

>> V = 1:3
V =
   1   2   3
>> V(2) = []
V =
   1   3

"How did you learn these things?"

By reading the documentation a lot, and by reading lots of answers on this forum. Also by experimenting: you could have easily tried it yourself, with a small 1x3 structure, and seen what happens.

"Could you suggest me a good tutorial, book, video to follow?"

Very few of the third-party books or online tutorials I have seen have been very good: some of them are quite dated, or teach inefficient practices, or give outright bad advice. The best source of information about learning MATLAB is... the MATLAB documentation:

https://www.mathworks.com/help/matlab/getting-started-with-matlab.html

https://www.mathworks.com/help/pdf_doc/matlab/index.html

You might like to read this:

https://www.mathworks.com/matlabcentral/answers/228557-experts-of-matlab-how-did-you-learn-any-advice-for-beginner-intermediate-users

Michael Daldini
Michael Daldini le 23 Avr 2018
Thank you again :) I could have tried but I was on the phone and i wanted to be sure to have a solution to the time I would get to the notebook :)
Thank you again, I will start reading a lot.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Debugging and Analysis 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!

Translated by