How to create a structure array without removing prior data?

Hello! I have an interface in matlab. I get the image and features of the image (name, last name, and ID) from the interface. Then I put it in the structure array, and then I save it in a mat file called "Database" by the "save" function. When I add another data (structure) to the structure array, the old data is removed from array. This is my code:
name=get(handles.edit2, 'string');
lastname= get(handles.edit3, 'string');
id= get(handles.edit4, 'string');
feature = handles.a;
feature1 = handles.phi;
array(3).amplitud=A;
array(3).angle=Phi;
array(3).name=name;
array(3).lastname=lastname;
array(3).ID=id;
save('Database','array');
I have put this code in the pushbutton_callback.
Thank you.
Mansoor

1 件のコメント

Mansoor ahmadi
Mansoor ahmadi 2015 年 1 月 5 日
I want to implement this project, you can see that in the below link:
if you can, give me some advice.thanks

サインインしてコメントする。

回答 (2 件)

Stephen23
Stephen23 2015 年 1 月 5 日
編集済み: Stephen23 2015 年 1 月 5 日

0 投票

Try using the '-append' option:
save(filename,variables,'-append')
More information can be found in the save documentation.

3 件のコメント

Mansoor ahmadi
Mansoor ahmadi 2015 年 1 月 5 日
thank you sir. But it dose not work.
Mansoor ahmadi
Mansoor ahmadi 2015 年 1 月 5 日
when i insert the data by command window it work good, but it dose not work in my interface.
Stephen23
Stephen23 2015 年 1 月 8 日
What does not work? How does it not work? Do you get any messages or outputs?

サインインしてコメントする。

Image Analyst
Image Analyst 2015 年 1 月 5 日

0 投票

Put a breakpoint right before you assign the first field to array(3). Check the values of the first and second structures by putting these lines in
array(1)
array(2)
Now do the lines where you assign fields to array(3).
array(3).amplitud=A;
array(3).angle=Phi;
array(3).name=name;
array(3).lastname=lastname;
array(3).ID=id;
Now put these lines again
array(1)
array(2)
whos array
The values for array(1) and array(2) should not vanish. You should not lose those structures. If they do, there's something you're not telling us, like you have a "clear" in there somewhere, or you're not incrementing the index correctly (like it's not really 3 but some variable instead).

6 件のコメント

Mansoor ahmadi
Mansoor ahmadi 2015 年 1 月 6 日
thanks sir!
I have tried this, I incremented the index from 1,2,3.... so on but dose not work.
Image Analyst
Image Analyst 2015 年 1 月 6 日
WHAT does not work? Are you saying that when you try to run the whos command it says that whos is an unknown function? Are you saying that when you put the name of the variable all by itself on the line it does not print the value of the variable to the command window?
Mansoor ahmadi
Mansoor ahmadi 2015 年 1 月 7 日
when I store the data in structure array "array(1).data" once it work good, when I store other "array(2).data" it also work good, But when I run the whos function it just shows the recent data that I stored "array(2).data" not all of them. like this:
array(1)
array(2)
whos array
array
ans=
[]
ans=
data
Or when I write in the command window like this:
array(1).data
ans=
[]
array(2).data
ans=
data
thank you. answer me please.
Image Analyst
Image Analyst 2015 年 1 月 7 日
Again, you either have a clear in there, or there's something you're not telling us.
And the whos command should not show that. When I do this:
array(2).data = [1,2,3];
whos array
array
it shows this:
Name Size Bytes Class Attributes
array 1x2 208 struct
ans =
1 2 3
which is different than what you showed.
Mansoor ahmadi
Mansoor ahmadi 2015 年 1 月 11 日
thanks Analyst!
Sir, when I store like this:
array(1).data=10; array(2).data=11; array(3).data=12; array(4).data=13; array(5).data=14;
when I want to access the "array(2).data or array(3).data" it dose not give me that's value(11 or 12); But I can access just the last value or "array(5).data"
thank you.
Answer me please.
Stephen23
Stephen23 2015 年 1 月 11 日
編集済み: Stephen23 2015 年 1 月 11 日
Using exactly the code from your comment above I can access any of the data values:
>> array(1).data=10; array(2).data=11; array(3).data=12; array(4).data=13; array(5).data=14;
>> array(2).data
ans =
11
>> array(3).data
ans =
12
This means there is something that your code is doing that you are not telling us about.
Also note that you can generate this structure in a neater way:
array = struct('data',num2cell(10:14));

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2015 年 1 月 5 日

編集済み:

2015 年 1 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by