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 (최근 30일)
a=load('data.txt'); b=sortrows(a); plot (b(2:1000,1),b(2:1000,2));

답변 (2개)

TastyPastry
TastyPastry 2016년 5월 27일
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
George  Makropoulos
George Makropoulos 2016년 5월 27일
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 2016년 5월 28일
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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 5월 27일
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);

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by