Ozan
Ozan

Reputation: 27

How can I fix the 'saveas' code to save pdf-files containing simulink simulation results by using 'strcat'?

I am struggled with the following code. I don't understand why the 'saveas' function doesn't work although I gave proper file name, file type and the figure of graphical simulation results of a Simulink model. Do I need to change something in 'strcat' code or? Also I hope that someone helps me in this issue.

function nightly_simulation_Callback(hObject, eventdata, handles)
open_system('SimulinkModel.slx');
sim('SimulinkModel.slx');
hFig = findall(0,'tag','SIMULINK_SIMSCOPE_FIGURE');
name = strcat('NCSSimResults','_',num2str(1));
saveas(hFig, name, 'pdf');

Resulted errors:

Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI('nightly_simulation_Callback',hObject,eventdata,guidata(hObject)) > Error while evaluating DestroyedObject Callback.


Upvotes: 0

Views: 211

Answers (1)

Phil Goddard
Phil Goddard

Reputation: 10772

You have multiple Scope blocks and hence hFig is a vector, which is what saveas is complaining about.

You need to have a loop, and save each figure/scope to a different file. So something like

hFig = findall(0,'tag','SIMULINK_SIMSCOPE_FIGURE');
for idx = 1:numel(hFig)
   name = strcat('NCSSimResults','_Scope_',num2str(idx));
   saveas(hFig(idx), name, 'pdf');
end

Or perhaps even better would be to get the name of the individual Scope blocks and use those as the name of each of the files.

Upvotes: 0

Related Questions