I have a simple question about matlab using interface.
I found that if I first declare an object (for example a table with the command UI table) NOT visible and after I make it visible, it works i.e. I see effectively in the figure the modification.
On the contrary, and here it's my problem, If I first declare an object visible and after I make it NOT visible, it doesn't work i.e. I not obtain the invisibility of the object.
Making an example:
figure;
h_tabell=uitable(gcf,'vis','off','data',randn(3));
h_tabell=uitable(gcf,'vis','on','data',randn(3)); %
here I see that the table is now visible
%Now I want make it that table invisible again, with
set(h_tabell,'vis','off') % here I see that the table already is visible
I need it, because in my program, the object is visible and if the user needs, I want to to set invisible the same object.
Anybody can help me?
You should only use the Matlab function set to change the properties of your object.
Here you make two calls to uitable, so here is what really happen if you break it down:
the first call create an invisible table
the second call create another table (overwriting the variable h_tabell with the new handle) but this time visible
You can verify this by checking that your figure now have two children:
children=get(gcf,'children');
Now if you try to change the visible property of the object referenced by the handle h_tabell, it will only apply to the second table.
The following piece of code works as expected and create only one table:
figure;
h_tabell=uitable(gcf,'visible','off','data',randn(3));
% Switch the table to visible
set(h_tabell,'visible','on');
% Switch it back to invisible
set(h_tabell,'visible','off');
Related
I wrote the following custom qgsfunction:
#qgsfunction(args='auto', group='Custom', referenced_columns=[])
def f1(currentfeature, feature, parent):
return "%s\n%s" % (repr(currentfeature),repr(feature))
In the config for the label I enter the following expression:
f1($currentfeature)
And here is the result:
I always believed that the $currentfeature would hold the same value as the implicitly passed argument feature, but they appear to be separate objects.
Can someone please either
confirm that they should be the same value, or
explain the difference between the two
(I also experience other odd things when executing my own custom functions, and I suspect something is not behaving as it should. Sometimes the addresss of the object changes when I zoom in or out, the address seems to cycle between af few values, and simply takes the next value in the cycle when I do any kind of zoom action. And the reason why I got to experiment with this in the first place is because I did not get the expected/correct attribute values back)
My goal is to build a 5x5 grid of images. In the following code, row, col and rowcol were created as variables local to the sprite, and newcol, newrow and cats are global. (By the way, is it possible to tell which variables are local and which are global? It's easy to forget or make mistakes.)
The result is a 5x1 grid only, as seen here.
I am unclear as to the order of execution of these statements. Does when I start as a clone get called before or after add_cat gets called the second time? My tentative conclusion is that it gets called afterwards, yet the clone's global variables seem to contain their values from beforehand instead.
When I attempted to debug it with ask and say and wait commands, the results varied wildly. Adding such pauses in some places fixed the problem completely, resulting in a 5x5 grid. In other places, they caused a 1x5 grid.
The main question is: How to fix this so that it produces a 5x5 grid?
Explanation
Unfortunately, the execution order in Scratch is a little bizarre. Whenever you edit a script (by adding or removing blocks, editing inputs, or dragging the entire script to a new location in the editor), it gets placed at the bottom of the list (so it runs last).
A good way to test this out is to create a blank project with the following scripts:
When you click the green flag, the sprite will either say "script one" or "script two", depending on which runs first. Try clicking and dragging one of the when green flag clicked blocks. The next time you click the green flag, the sprite will say whichever message corresponds to the script you just dragged.
This crazy order can make execution incredibly unpredictable, especially when using clones.
The solution
The only real solution is to write code that has a definite execution order built-in (rather than relying on the whims of the editor). For simpler scripts, this generally means utilizing the broadcast and wait block to run particular events in the necessary order.
For your specific project, I see two main solutions:
Procedural Solution
This is the most straightforward script, and it's probably what I would choose to go with:
(row and col are both sprite-only variables)
Because clones inherit all sprite-only variable values when they are created, each clone will be guaranteed to have the correct row and col when it is created.
Recursive Solution
This solution is a bit harder to understand than the first, so I would probably avoid it unless you're just looking for the novelty:
I am an engineering student, fairly new to MATLAB. I have created a GUI for a class that calculates voltages and amperages of a specified circuit. I wish to display the amperages as (A) and (mA). The program currently calculates the data and displays it in static text boxes. I am using a button group with two radio buttons inside, working exclusively. I have used selectionChangeFcn in the following manner to control the buttons.
function group_SelectionChangeFcn(hObject, eventdata, handles)
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object.
case 'radiobutton1'
var=1;
set(handles.text1, 'String', '(A)');
case 'radiobutton2'
var=1000;
set(handles.text1, 'String', '(mA)');
otherwise
end
Choosing one button or the other changes text in the static text boxes, and assigns a value to a variable. The bulk of the programming code is executed under a pushbutton. All of the variables are contained in this code, and are filled from edit boxes. Everything else works great so far. Within the cases I have (A) or (mA) outputted to a static text box, and you can see that operating the buttons does in fact display different values.
My problem is this; I wish to use variable var in the code to multiply my answer data so that it reads in either A or mA. Like this;
set (handles.text36,'string',num2str(ir1*var,'%20.3f'))
I cannot get this to work, however, the error says that var is undefined. It seems to do this in all circumstances. I have experimented with moving the code to different locations, but I cannot get it to work. Any help or ideas would be appreciated.
From what it looks like var is defined within the switch statement, thus it’s scope is just inside of the switch. You should define var outside of the switch to make its scope open to the function.
Also, when writing code in stack overflow, have a line break between your paragraph and indent the code 4 spaces and it’ll be formatted like a block of code, see below. Finally, include line breaks so one can determine easily where your lines of code are.
switch (...)
methods
end
You might profit from reading these articles:
http://www.mathworks.de/de/help/matlab/matlab_prog/base-and-function-workspaces.html
http://www.mathworks.de/de/help/matlab/creating_guis/ways-to-manage-data-in-a-guide-gui.html#f5-998711
You'll have to store var somewhere where it's accessible from other gui-functions.
One example:
Change & store var in your first function, e.g. as part of the handles structure:
% set var as a field on handles
handles.var = 1000;
% save the guidata - don't miss this!
guidata(hObject, handles);
In your second function, that's supposed to use var you can now take var from the handles structure:
set(handles.text36,'string',num2str(ir1*handles.var,'%20.3f'))
Based on the idea that your set line is not in the same function as the other methods, you must have the var either defined globally, sent through the function calls, or saved to handles or some other object. Here is a method of saving data that can be accessed from any function.
First, save the handle to the main gui, to the root address. Call this in the Main_OpeningFcn of your program.
setappdata(0, ‘mainGUI’, gcf);
Then, in your code call
mainGUI = getappdata(0, ‘mainGUI’);
var = someValue;
setappdata(mainGUI, ‘var’, var);
This saves the var variable inside of mainGUI. Now you have access to var from wherever you want just by calling getappdata. You do have to get mainGUI first, but that is only one line. Doug Hull has a video on MatLabCentral. I can find the link for you later that discusses this in more detail. One benefit with this method is that you don’t have to keep passing the handle structure around if you don’t need it.
I am currently working on a matlab gui and after some beginner's problems with the data handling I am quite satisfied with the result.
There is just one hiccup: whenever the program is done running, the gui becomes unresponsive and the buttons and text elements vanish, all I can see is the background.
I have scanned the functions thoroughly for close all; statements and such, but there is nothing there.
How do I return 'clean' to the gui so I can put in more data? Do I need to put the gui in a constant while loop?
best wishes
Chris
You can do the following:
Modify the attribute of your controls to be interruptible:
set(handles.figure, 'Interruptible','on');
Create a callback function based on pressing a determined key combination.
set(KeyPressFcn, #resume_fcn);
Create a callback function that solves the problem.
function resume_fcn()
if eventdata.Key = ...
exit;
end
end
However, the consistency of data may be lost. In case you don't want to return 'clean' to the gui, you can type:
delete(get(0,'Children'))
I've got a MATLAB GUI that has different aspects of functionality, each with their own panel of uicontrols. When one panel is selected, the other one is set to invisible, and vice-versa. However, they share some of the same inputs in the form of a popup menu. Can I include a 'clone' instance of the menu on the second panel somehow? I'd like to avoid as many redundant callbacks and uicontrols as possible.
I guess if the uicontrol was a direct child of the figure, you may be able to put it in front of everything.
A much simpler solution is to use the same callback for multiple uicontrols. In the property editor, you can modify the callback name and set it to a common callback function. Additionally, you can create a field (e.g. myPopupH) in the OpeningFcn of the GUI, in which you store the handles of the popups that should behave the same way. Then, in the callback, you'd use hObject, i.e. the first input argument, for all the get calls (to access the modified state of the popup-menu), but you'd use handles.myPopupH in all the set calls, so that you can ensure that both popups always have the same state. Thus, the ui-object may be redundant, but all the code (which is much more critical) only exists in a single copy.
One place where I routinely use a single callback for multiple ui elements is the close request function which is accessed from the "Cancel"-button as well as from the "X" that closes the figure, and possibly from one of the "File"-menu items.