I am new to Matlab and i am searching for the correct code to make a plot push button into GUI after loading data from a file (another push button) .The plot i wanna do is the following but i stuck in callback functions etc

1 vue (au cours des 30 derniers jours)
a=load('data.txt'); b=sortrows(a); plot (b(2:1000,1),b(2:1000,2));

Réponses (2)

TastyPastry
TastyPastry le 27 Mai 2016
So you need two callback functions in order to do this. The first callback will load your data and the second will plot your data.
You can set callbacks like this:
set(handles.loadData,'callback',@loadDataFcn);
set(handles.plot,'callback',@plotFcn);
Then your load data callback will look like this:
function loadDataFcn(varargin)
handles = guidata(gcf);
handles.myData = load('myfile');
guidata(gcf,handles);
end
Plotting:
function plotFcn(varargin)
handles = guidata(gcf);
axes(handles.axes); %this brings the axes into focus
plot(handles.myData);
end
You can adjust the figure handle of your main GUI window to include a "myData" field so you can more easily pass data.
  6 commentaires
George  Makropoulos
George Makropoulos le 27 Mai 2016
this worked great thanks!! one more question after loading the file how I sort the rows of the data and then plot the sorted ones.
Image Analyst
Image Analyst le 28 Mai 2016
Well again, I have a different opinion than Tasty. GUIDE is definitely not the best, or slickest dialog box utility - not in a class with Microsoft Studio by a longshot - but for anything more than the simplest GUI, I think it's the way to go. My apps typically have about 20 - 30 controls on them, and if I were to try to build it myself, setting the height, width, location, font size, font weight, string, value, min, max, step size, etc. it would be tedious beyond belief. I'd have to hand code in hundreds of properties. If you want to go beyond the very basics in MATLAB you'll have to learn how to use GUIDE or App Designer. Fortunately it's not hard and you can master it in a few minutes after watching the tutorial link I just gave.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 27 Mai 2016
I disagree. I think you should do it all in one callback, not two. Have one button that says "Load data..." and then call load() and plot() all in the same callback, unless there is some strange reason why you'd want to load the data and NOT plot it. I mean, what is the purpose for making this into a two-step procedure when it doesn't need to be?
The reason why your code didn't work was because the badly-named "a" is actually a structure - that's what load returns. And hanging off the structure are all the variables you saved - they're fields/members of the structure. So to get them you'd do this:
storedStructure = load(matFileName);
unsortedData = storedStructure.a; % Extract "a" from the structure into a variable called "unsortedData"
sortedData =sortrows(unsortedData);
x = sortedData(2:1000,1);
y = sortedData(2:1000,2);
plot(x, y, 'LineWidth', 2, 'MarkerSize', 10);
grid on;
title('Sorted Data', 'FontSize', 20);
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);

Catégories

En savoir plus sur Migrate GUIDE Apps 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