Saving a Structured Array (containing plot parameters) to a mat file (2024)

22 views (last 30 days)

Show older comments

Jason on 24 Jun 2024 at 14:17

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2131476-saving-a-structured-array-containing-plot-parameters-to-a-mat-file

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2131476-saving-a-structured-array-containing-plot-parameters-to-a-mat-file

Commented: Jason on 24 Jun 2024 at 14:48

Accepted Answer: Stephen23

Open in MATLAB Online

Hi, Im trying to save plot data by using a structured arra

ax=app.UIAxes;

ax.Children;

h1 = findall(ax, 'type', 'line'); % Line objects

nlines=numel(h1);

Assign the desired plot parameters to a struct "S"

for i=1:nlines

thisLine=h1(i);

S(i).Xdata=thisLine.XData; S(i).Ydata=thisLine.YData;

S(i).Colour=thisLine.Color;

S(i).LineStyle=thisLine.LineStyle;

S(i).LineWidth=thisLine.LineWidth;

S(i).DName=thisLine.DisplayName;

end

Save to a .MAT file

try

[file,folder]=uiputfile({'*.mat','Matlab Files'},'Save Data',app.startfolder);

catch

[file,folder]=uiputfile({'*.mat','Matlab Files'},'Save Data','C:\');

end

app.startfolder=folder;

savepath=fullfile(folder,file);

save(savepath,"-struct",'S')

But I keep getting this error:

Error using save

The argument to -STRUCT must be the name of a scalar structure variable.

I have checked the class of S and the fieldnames contained within it:

fields = fieldnames(S)

class(S)

fields =

6×1 cell array

{'Xdata' }

{'Ydata' }

{'Colour' }

{'LineStyle'}

{'LineWidth'}

{'DName' }

ans =

'struct'

Thanks for any help

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Stephen23 on 24 Jun 2024 at 14:25

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2131476-saving-a-structured-array-containing-plot-parameters-to-a-mat-file#answer_1476386

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2131476-saving-a-structured-array-containing-plot-parameters-to-a-mat-file#answer_1476386

Edited: Stephen23 on 24 Jun 2024 at 14:28

Open in MATLAB Online

Explanation: your structure is not scalar: it has nlines elements. The -struct option only works with scalar structures.

Error using save

The argument to -STRUCT must be the name of a scalar structure variable.

% ^^^^^^

Solutions:

  • either get rid of the -struct option, or
  • create a scalar structure (by making the fields arrays, not the structure).

Tip: always LOAD into an output variable!

3 Comments

Show 1 older commentHide 1 older comment

Jason on 24 Jun 2024 at 14:29

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2131476-saving-a-structured-array-containing-plot-parameters-to-a-mat-file#comment_3194546

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2131476-saving-a-structured-array-containing-plot-parameters-to-a-mat-file#comment_3194546

Edited: Jason on 24 Jun 2024 at 14:33

Open in MATLAB Online

Thanks Stephen, so for this option " create a scalar structure (by making the fields arrays, not the structure)", you mean

for i=1:nlines

thisLine=h1(i);

S.Xdata(i)=thisLine.XData; S.Ydata(i)=thisLine.YData;

S.Colour(i)=thisLine.Color;

etc

end

Thanks for tip, but Im not sure I follow.

Thanks

Jason

Steven Lord on 24 Jun 2024 at 14:44

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2131476-saving-a-structured-array-containing-plot-parameters-to-a-mat-file#comment_3194571

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2131476-saving-a-structured-array-containing-plot-parameters-to-a-mat-file#comment_3194571

Open in MATLAB Online

Since several of the properties you're trying to store in the struct are non-scalar, it won't work as you wrote it. But you could have the fields of the scalar struct contain cell arrays, where each cell in the array can have data of different sizes depending on the properties of the specific line you're trying to store inside it.

h = plot(1:10, 1:10, 'r-', 1:7, 10:-1:4, 'k:');

Saving a Structured Array (containing plot parameters) to a mat file (5)

for k = 1:numel(h)

S.XData{k} = h(k).XData;

S.YData{k} = h(k).YData;

end

disp(S)

XData: {[1 2 3 4 5 6 7 8 9 10] [1 2 3 4 5 6 7]} YData: {[1 2 3 4 5 6 7 8 9 10] [10 9 8 7 6 5 4]}

isscalar(S) % yes

ans = logical

1

isstruct(S) % also yes

ans = logical

1

cd(tempdir)

save('mydata.mat', '-struct', 'S')

whos -file mydata.mat

Name Size Bytes Class Attributes XData 1x2 344 cell YData 1x2 344 cell

P = load('mydata.mat')

P = struct with fields:

XData: {[1 2 3 4 5 6 7 8 9 10] [1 2 3 4 5 6 7]} YData: {[1 2 3 4 5 6 7 8 9 10] [10 9 8 7 6 5 4]}

isequal(P, S)

ans = logical

1

Jason on 24 Jun 2024 at 14:48

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2131476-saving-a-structured-array-containing-plot-parameters-to-a-mat-file#comment_3194576

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2131476-saving-a-structured-array-containing-plot-parameters-to-a-mat-file#comment_3194576

Thats fantastic, thankyou!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsGraphics ObjectsGraphics Object Identification

Find more on Graphics Object Identification in Help Center and File Exchange

Tags

  • struct
  • mat
  • save

Products

  • MATLAB

Release

R2023b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Saving a Structured Array (containing plot parameters) to a mat file (7)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Saving a Structured Array (containing plot parameters) to a mat file (2024)
Top Articles
Exercices de 6ème maths en ligne
The Alexandria Times-Tribune from Alexandria, Indiana
Scammer phone number lookup. How to check if a phone number is a scam
Missing 2023 Showtimes Near Amc Classic Florence 12
Ap Psychology Unit 8 Vocab
Umc Webmail
Lvc Final Exam Schedule
Allegheny Clinic Primary Care North
Whmi.com News
Accident On May River Road Today
Www. Kdarchitects .Net
Care Guide for Platy Fish – Feeding, Breeding, and Tank Mates
Watch Valimai (2022) Full HD Tamil Movie Online on ZEE5
Blaire White's Transformation: Before And After Transition
Sound Of Freedom Showtimes Near Sperry's Moviehouse Holland
Sites Like SkiptheGames Alternatives
Star Rug Aj Worth
Long-awaited Ringu sequel Sadako doesn’t click with the 21st century
Monster From Sherpa Folklore Crossword
Appleton Post Crescent Today's Obituaries
15:30 Est
Sophia Garapetian Twitter
Red Lobster cleared to exit bankruptcy under new owner Fortress
Kentucky Lottery Scratch Offs Remaining
Greenville Daily Advocate Greenville Ohio
Female Same Size Vore Thread
Space Coast Rottweilers
Bay State Neurology
Seanna: meaning, origin, and significance explained
Wilson Tattoo Shops
Mercedes E-Klasse Rembekrachtigers voorraad | Onderdelenlijn.nl
Black Boobs Oiled
855-392-7812
Food Handlers Card Yakima Wa
Mikayla Campinos: The Rising Star Of EromeCom
Pain Out Maxx Kratom
Witchwood Icon
Gunblood Unblocked 66
Was Man über Sprints In Scrum-Projekten Wissen Sollte | Quandes
Black Myth Wukong All Secrets in Chapter 6
Commuter Rail Gloucester
Brgeneral Patient Portal
Netdania.com Gold
Craigs List New Haven Ct
Www.1Tamilmv.cfd
Ny Lottery Second Chance App
Sinmiedoalban12
Tampa Catholic Calendar
A1.35.3 Spanish short story: Tending the Garden
50 Shades Of Grey Movie 123Movies
James in Spanish | Spanish to Go
Vorschau: Battle for Azeroth – eine Tour durch Drustvar
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 5897

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.