Why does my matrix looks empty when it's not?

Hello, I'm trying to code an UItable where the user is free to add and delete as many rows as they want to. I need the data entered by the user to do some calculus but when I use the get function and then transfer that to a matrix, it runs ok but shows an empty matrix M.
Ps. My add and delete row work just fine, the problem is somewhere else
tabla={'','',''};
app.UITable.Data=tabla;
%I called "tabla" as a global because the code above is in a
%startupFCN button
global tabla
datos = get(tabla, 'Data');
M= cell2mat(datos);

 Accepted Answer

When you global a variable after assigning a local value to the variable in the same workspace, then the local value is removed and the current global value replaces it.
tabla={'','',''};
app.UITable.Data=tabla;
global tabla
is like clear tabla right before the global tabla -- unless, that is, tabla was already global in the workspace.
Furthermore, you get(tabla, 'Data') but is tabla a UITable ? Or is it a cell array of character vectors like you assigned in your code?
Your code appears to be confusing the contents of the UITable and the handle of the UITable.

5 Comments

yes, tabla is a UITable. I'll attach a screenshot so it's clearer. I tried to write "global tabla" as you sugested but I'm still getting an empty matrix.
You have
tabla={'','',''};
app.UITable.Data=tabla;
global tabla
in your startupFcn. That is equivalent to
temporary_variable = {'','',''};
app.UITable.Data = temporary_variable;
clear tabla
global tabla
tabla = evalin('global', 'tabla');
Except that evalin('global') is not a real function call. The intent is to show that the global value of tabla is being fetched and written into the local workspace in startupFcn . Chances are that the global variable did not exist, and that the global variable inside the startupFcn will probably end up empty.
And then the function returns, and the only difference will be that whereas before the global tabla probably did not exist, that now the global tabla does exist and is empty.
Then when you reach calculateButtonPushed, and you have the global tabla the empty value will be fetched, and you will try to get([], 'Data') Interestingly, that will not generate an error message and will instead return empty. Then you cell2mat() the empty and get empty into your M.
I firmly recommend against using tabla as both the contents of the table and also the handle of the table.
I do not understand why you are saving what is intended to be the handle of the table in a global variable: why not just access the app.UITable.Data in your calculateButtonPushed function ?
I was doing that way by mistake, I've never used the UITable function before and I did it wrong. I took your recommendation but now it says "Conversion to double from cell is not possible."
function calculateButtonPushed(app, event)
datos = app.UITable.Data;
M = cell2mat(datos);
end

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!