You are on page 1of 2

[all about matlab]

January 23, 2013

HOW TO STORE DATA IN AXES FIGURE USERDATA IN MATLAB

A good Matlab Programmer while dealing with GUI will avoid using GLOBAL syntax to declare variables used in the program. The problem is Matlab will preserve much memory when you declare variables in each sub_callback. So one has to optimize using every properties that the control has. Usually, we use USERDATA property to store array or single data in Matlab GUI. So we can access the data anytime. But there is a case when we deal with userdata property of axes figure. This is not as usual as property of others control. So I need to write this article to help whether a reader face the same problem. This is not a big issue actually. What you have to do is to avoid putting the syntax before plot command. Because plot command in matlab will reset all the value in USERDATA of current axes. Please see some example below: I want to display my audio signal data to fig1 which is axes object/control. In the same sub_callback, I want to save my data into current axes USERDATA. So what I think best to do is:
h1 = handles.fig1; set(h1, 'UserData', data); plot(h1, (1:length(data))/fcuplik, data); set(h1, 'YAxisLocation', 'Right'); xlabel(h1, 'Waktu (detik)'); ylabel(h1, 'Amplitudo'); set(h1, 'Color', [0.945 0.969 0.949]); set(h1, 'XColor', [0.847 0.161 0.0]); set(h1, 'YColor', [0.847 0.161 0.0]);

this is absolutely wrong. Take a look at the row which is dash boxed. I found this is not good placement of the code. Because the final result would be an empty cell. So we have to change its placement. See below, please:
h1 = handles.fig1; plot(h1, (1:length(data))/fcuplik, data); set(h1, 'YAxisLocation', 'Right'); xlabel(h1, 'Waktu (detik)'); ylabel(h1, 'Amplitudo'); set(h1, 'Color', [0.945 0.969 0.949]); set(h1, 'XColor', [0.847 0.161 0.0]); set(h1, 'YColor', [0.847 0.161 0.0]); set(h1, 'UserData', data);

[janshendry@gmail.com]

Page 1

[all about matlab]

January 23, 2013

when I changed the order of the row, this give desired output. So I can access the content of USERDATA from any sub_callback in matlab gui. Happy try

Picture above shows you how this is work. @thankss

[janshendry@gmail.com]

Page 2

You might also like