Exporting signal data from figure in Matlab - matlab

Is it somehow possible to get signal data from figure, to save the vector or matrix of the data to the Workspace?
We happend to make a lots of measurements on a real system in school, but ve saved only figures of the measutrement and now we need to use some of the signals from the figure and use them in another figure for comparison.

You can load the figure in Matlab and go to View->Properties, to pull the data out of the plot's properties e.g. for a line graph plot:
You can get at the XData and YData properties and copy/paste the values of out it e.g.
Alternatively, as I had to do once when this method failed, you can save the figure as EPS/postscript and try to pull the data out of the postscript file in a text editor (!)

Related

how to deal with big memory footprint of plots

I'm doing some simulations that end up taking quite a bit of memory. The numbers themselves are ok for my machine though. But when I try to plot them, I run out of memory. I guess matlab's plots are a special format that makes use of all the data available. However, I'd like to skip that step, and just generate a .jpg or .png directly. I don't even need/want the plot to pop up on the screen, I'd rather just save it directly to file, and bring it up later when I want.
Is such a thing possible in matlab?
Try creating the figure as invisible:
figure('visible','off')
plot(x, y); %// Insert your plot commands here
print filename.png -dpng %// print figure to file

Copy specific axes from figure to new figure

Ive saved a figure as a .fig-file in MATLAB which I did now reopen after some time.
Is there a way to access the data which is saved in the Histogram? I want to replot it by using the hist() command instead of imhist into a new figure (the reason is that matlab2tikz cant export the histogram plotted by imhist properly).
I imagine I could access the data when I would know the handle of the histogram, right?
EDIT:
A = findall(gcf,'type','axes');
then inspecting
get(A(i))
to see which axes the histogram is plotted in. This works but I have to figure out how to retrieve the actual data.
But I somehow assume that I have to look at a parent/children of the axes handle (depending which hierarchy MATLAB creates of objects).
Okay I figured it out finally.
As written in my edit above, you can use findall to find the handles of all axes-objects.
After using it, try to find out which handle refers to which axes by looking at the entries like X/YLim in get(A(i)), after finding the axes-ID and storing it (the k-th element in A) to idx = A(K), use this script to read the entries from the histogram plotted by imhist() -> The values are replicated as often as described by the bins (YData) and then replotted by hist into a new figure:
% ----------------------------------------------------------------------- %
b = get(idx);
b = get(b.Children); % Get the Plot-Handle
x = b.XData; % Bins
y = b.YData; % Bin-Counts
data = [];
for i = 1:length(x)
data = [data x(i)*ones(1,y(i))]; % replicate data
end
figure
hist(data, length(unique(x)));
xlim([min(data) max(data)]);
Edit: The for-loop is a quick and dirty one ;-) Im sure there's a nicer solution e.g. by using repmat, but I was only interested in a quick solution :-)

Matlab Simulink graph plotting

I plotted a signal using "To Workspace" in Simulink Matlab. Now I want to take the mean of the specific part of that signal which I plotted.How can I extract values from "To Workspace" or how can I take the mean of the specific area of that graph.
In "To workspace" you define a variable name, let's say: "simout"
I made a simple simulink as the following:
you can save with different formats: Timeseries, Structure with time, Structure, Array.
Then, when you run the simulink, it will save the variable in the worksapce as a structure.
Then you can use the variable to plot the data inside. check this example :
consider you saved using Structure with time you can get data like this:
t = simout.time
x = simout.signals.values
and you can plot the data:
plot(t,x)

Matlab get an average plot out of several plot

I'm just getting started with matlab and I'm trying to plot some graph with it.
The problem is I don't know how to get the average data out of 10 plot().
Can anyone guide me for it? Thank you :)
Assuming you don't have access to the original data you used for doing the plots:
plot_data = get(get(gca,'Children'),'YData'); % cell array of all "y" data of plots
average = mean(cell2mat(plot_data));
In order for this to work, you have to use this code right after doing the plots, that is, without plotting to any other figure (gca is a handle to the current axes).
Assume your data is stored row-wise in a m x n matrix A, with n columns corresponding to different values of the continuous error, and m rows corresponding to different curves. Then to inspect the mean over the curves just use
Amean = mean(A,1);
plot(Amean)
Please take a look at this link: it solve my problem getting the average plot.
https://www.mathworks.com/matlabcentral/fileexchange/27134-plot-average-line
After downloading the files just put those script on your working folder and add this line to your script.
plotAverage(gca,[],'userobustmean',0)

MATLAB: Stacking .figs

I'm currently working on a study about fuel consumption. I have multiple .fig files that shows the trend of fuel consumption in L/100 Km versus Time. I have multiple cases showing the behavior of the plot under different conditions, and I wan't to show the differences between them. An example of the plot is shown below:
Is there anyway to stack plots from different .fig files in 1 .fig file?
Ideally, you'd want to generate those different plots using subplot.
Your exact question has been answered by ZiV in the mathworks forums:
One way to do this is to extract xdata and ydata from existing
figures, then re-plot these data in a new figure window as desired.
For example,
open('gugu.fig');
h_gugu=get(gca,'Children');
x_gugu=get(h_gugu,'XData');
y_gugu=get(h_gugu,'YData');
open('lulu.fig');
h_lulu=get(gca,'Children');
x_lulu=get(h_lulu,'XData');
y_lulu=get(h_lulu,'YData');
figure
subplot(121)
plot(x_gugu,y_gugu);
subplot(122)
plot(x_lulu,y_lulu)
saveas(gcf,'gugululu','fig')