How to pass the value of a table to a matrix - matlab

Im trying to do a GUI in matlab that accepts the values in a table to converting it to a matrix, but the idea is that a user can set the number of rows and columns first.
The panel looks like this
and the code for the push button is
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
rows =str2double(get(handles.edit_rows,'String'));
cols=str2double(get(handles.edit_cols,'String'));
num_elem=cell(rows,cols);
num_elem(:,:)={"};
set(handles.uitable1,'Data',num_elem)
set(handles.uitable1,'ColumnEditable',true(1,cols))
But then, how can export or convert to a matrix so I can apply functions to it?
UPDATE
With the help of byetisener I updated the code to
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
filas=str2double(get(handles.edit_fila,'String'));
column=str2double(get(handles.edit_col,'String'));
num_elem=cell(filas,column);
num_elem(:,:)={''};
set(handles.uitable1,'Data',num_elem)
set(handles.uitable1,'ColumnEditable',true(1,column))
handles.uitable1.Data = cell(filas, column);
matrix = cell2mat(handles.uitable1.Data);
matrix
but this is giving an empty matrix
It is not taking the values of the cells, it is supposed that the button resizes and copy the values at the same time, if not how con copy in another button once the matrix is resized?

There are some problems about your code:
You do not really assign values here, you are just setting the Data of the uitable to an array of empty cells.
num_elem =
1×2 cell array
{0×0 char} {0×0 char}
If you ever succeed, you code will write everything you want to only the first column of the uitable. Because you are not iterating through rows. The pushbutton only adds to the first row.
cell2mat() function won't work if you have different data types in your table. You may think that you do not have different data types, but empty cells are type cell and the data you enter is type double, so there it is.
To solve all of this I have rewritten a callback function for you. You may directly paste this code to your callback, replacing yours. I should give you the matrix you want at the end, it does in my computer.
filas = str2double(handles.edit_fila.String);
column = str2double(handles.edit_col.String);
% This loop looks for an empty row to write new data
for i = 1:length(handles.uitable1.Data)
if isempty(handles.uitable1.Data{i,1})
handles.uitable1.Data(i,1) = {filas};
handles.uitable1.Data(i,2) = {column};
break;
else
disp('Error occured');
end
end
% This double for loop check if there are any empty cells
% if it finds one, it changes it to 0, so all the cells have the same type
for i = 1:length(handles.uitable1.Data)
for j = 1:2
if isempty(handles.uitable1.Data{i,j})
handles.uitable1.Data(i,j) = {0};
else
disp('Error occured');
end
end
end
matrix = cell2mat(handles.uitable1.Data); % The matrix you want
Just check if all the variable names are the same and do not forget to accept is as an answer. Hope it helps.

I am not sure if this answers you question but you can follow this approach.
First of all, if you are interested, using dot notation is faster in MATLAB than setter and getter methods.
So, what you can do is:
handles.uitable1.Data = cell(rows, cols);
or, of course, alternatively:
set(handles.uitable1, 'Data', cell(rows,cols));
If what you want to is to convert the data in uitable to a matrix, you can use:
matrix = cell2mat(handles.uitable1.Data);
If you table contains non-numeric values:
tableData = handles.uitable1.Data;
tableData = [str2double(tableData(:, 1)), cell2mat(tableData(:, 2))];
Hope this helps. Let me know if you solve your problem.

Related

Delete selected rows in uitable - Matlab

I built in GUI (using GUIDE) a uitable (4x5) that last row is logical so I can select lines to delete.
d = {'L1',1,10,true;'L2',2,20,true;'L3',3,30,false;'L4',4,40,true;'L4',5,50,false};
set(handles.outputTable,'Data',d)
I created a button to delete the selectd rows but I does not work:
function deleteButton_Callback(hObject, eventdata, handles)
% hObject handle to deleteButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
dataTable = (get(handles.outputTable,'data'));
[m n] = size(dataTable);
disp(dataTable);
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=[];
end
end
disp('Modifed table')
disp(dataTable);
How can I fix it so I get set the table again in the GUI?
This is wrong:
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=[];
end
end
First of all, if num2str(cell2mat(dataTable(i,4))) =='1' is a convoluted equivalent of if dataTable{i,4}==1. You should learn to use curly braces {} to access the content of a cell array.
Then, it will work only if the counter is decreased.
See what happens:
Test if row n should be deleted
Delete line n; the content of row (n+1) have now moved to row n
Increment counter i from value n to n+1
The row now at position n has never been tested for deletion !
What was at row (n+1) is never tested, since the delete operation moves it backwards first, then the counter is incremented without testing again. The solution is to decrement the counter.
for i = m:-1:1
if dataTable{i,4}
dataTable(i,:)=[];
end
end
The rows moved by the deletion operation have already been tested, so in the end it is certain that all lines will have been tested.
Now, the same can be obtained in a vectorized form with a single line:
dataTable = dataTable(cell2mat(dataTable(:,4))==0,:);
The whole function boils down to:
function deleteButton_Callback(hObject, eventdata, handles)
dataTable = get(handles.outputTable,'data');
% Do some checks to make sure that the values input by are correct %
assert(all(cellfun(#isscalar,dataTable(:,4))), 'Last colum should contain scalars!');
set(handles.outputTable,'data' , dataTable(cell2mat(dataTable(:,4))==0,:));
end

Graphing with GUI

Using
x=-10:0.1:10
f=x+2
in basic m-file works fine.
But now I am trying to draw a plot using GUI, and by inputing a function.
It gives me bunch of errors. Could anyone explain how I can give values to the y when I have the range of x set?
% --- Executes on button press in zimet.
function zimet_Callback(hObject, eventdata, handles)
% hObject handle to zimet (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x=-10:0.1:10;
f=inline(get(handles.vdj,'string'))
y(x)=f
axes(handles.axes)
plot(x,y)
color=get(handles.listbox, 'value')
switch color
case 2
set(plot(x,y),'color', 'r')
case 3
set(plot(x,y),'color', 'g')
case 4
set(plot(x,y),'color', 'b')
end
style=get(handles.popupmenu, 'value')
switch style
case 2
set(plot(x,y), 'linestyle','--')
case 3
set(plot(x,y), 'linestyle','-.')
case 4
set(plot(x,y), 'linestyle',':')
end
rezgis=get(handles.grid, 'value')
switch rezgis
case 1
grid on
case 2
grid off
end
Notice that according to the inline function documentation this function will be removed in future release; you can use, instead, anonymous functions (see below).
The inline function require, as input a string of characters while the get function returns the text of the edit box as a cellarray, therefore you have to convert it using the char function.
Also, once you have generated the inline object, it is you function, so you have to use it directly.
Using inline
You have to change your code this way:
x=-10:0.1:10;
% f=inline(get(handles.vdj,'string'))
% y(x)=f
f=inline(char(get(handles.vdj,'string')))
y=f(x)
axes(handles.axes)
ph=plot(x,y)
Using an anonymous function
You can achieve the same result by using anonymous functions this way:
x=-10:0.1:10;
% Get the function as string
f_str=char(get(handles.vdj,'string'))
% add #(x) to the string you've got
f_str=['#(x) ' f_str ];
% Create the anonymous function
fh = str2func(f_str)
% Evaluate the anonymous function
y=fh(x)
axes(handles.axes)
ph=plot(x,y)
Edit
You can fix the problem with the setting of the line color and style this way:
modify the call to plot by adding the return value (it is the handle to the plot (see above: ph=plot(x,y))
chance the calls to set by replacing the call to plot with the handle of the plot itself (the ph variable as above)
So, to change the color and the line style, in your switch section:
set(ph,'color','r')
set(ph,'linestyle','--')
Hope this helps,
Qapla'

writing elements in matlab listbox

I am trying to write a list in a listbox.
code:
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
error = getappdata(0, 'error_norm');
rows = size(error,1);
for i = 1:rows
set(handles.listbox1,'string',strcat('filt_',num2str(i)));
for j = 1:length(error)
set(handles.listbox1,'string',strcat('sig_',num2str(i),'_',num2str(j)));
for k = 1:length(error{j}.main)
set(handles.listbox1,'string',strcat('seg_',num2str(i),'_',num2str(j),'_',num2str(k)));
end
end
end
Where error is a array of structure, this array contains filters, singals in these filters, segments of these signals. based on the number of all these components, i want to write the list. I want to write something like this in the listbox:
filt_1
sig_1_1
seg_1_1_1
seg_1_1_2
sig_1_2
seg_1_2_1
seg_1_2_2
But apparently, 'set' function overwrites the elements, so all i am getting is 1 element and the last element.
Any suggestion to how to overcome this problem will be appreciated.
Yes, since set always overwrites the string, it is better to firstl build the string and then pass it to set.
Example
% Sample data
rows=4;
error=cell(1,5);
for i=1:length(error)
error{i}.main=rand(1,4);
end
% Build string
str={};
for i=1:rows
str{end+1}=sprintf('filt_%i',i);
for j=1:length(error)
str{end+1}=sprintf('sig_%i_%i',i,j);
for k=1:length(error{j}.main)
str{end+1}=sprintf('seg_%i_%i_%i',i,j,k);
end
end
end
% Set data
set(handle.listbox1,'String', str);
Depending on the size of the final string it might be a good idea to preallocate str for performance.

MATLAB: Cannot parse multiple floating-point values

I have a GUI with a UITable (built in GUIDE). I read in two numerical values and through a series of steps convert the floating-point value to a string. I want the user to have the ability to click on a specific cell in the UITable containing the two values (now as strings), and read those values back as floating-point values. For whatever reason, I can only ever get my code to read in the first floating-point value. My code (in order) is below.
Step 1: Access data and convert to string and place string in corresponding column.
function fillAnnotRangeInfo(obj, selectedAxes)
selectedAxesTag = selectedAxes.Tag;
rawAxesTag = obj.rawDataDisplayAxes.Tag;
psdAxesTag = obj.psdDataDisplayAxes.Tag;
% Depending on which axes the user clicks on, the units will either be Hz or s
if strcmp(selectedAxesTag, rawAxesTag)
dataRange = strcat(num2str(obj.t1), {'s'}, {' - '}, num2str(obj.t2), {'s'});
elseif strcmp(selectedAxesTag, psdAxesTag)
dataRange = strcat(num2str(obj.t1), {'Hz'}, {' - '}, num2str(obj.t2), {'Hz'});
end
obj.nextRow.AnnotRange = dataRange;
end
Step 2: Determine if user clicked in correct cell and attempt to read two floating-point values out.
% --- Executes when selected cell(s) is changed in existingAnnotationsTable.
function existingAnnotationsTable_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to existingAnnotationsTable (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
% AE = handles.UserData;
Indices = eventdata.Indices;
% Determine if column is column of interest
if Indices(2) == 4
rangeData = handles.existingAnnotationsTable.Data;
rangeData = rangeData{Indices(1), Indices(2)};
annoRange = sscanf(rangeData, '%f')
else
end
return
Ultimately, the result I get is if I have a string exactly as follows: "7.4250Hz - 32.502Hz" (or replace Hz with "s"), my program will only produce "7.4250". Nothing more nothing less. I've tried textscan, sscanf, and strread. Each one I explicitly set my filter to floating-point (%f). With strread I tried setting it to cycle through more than once (strread('string', %f, 2)). I have no idea what else to do or try or change.
REGARDING THE ANSWERS BELOW FOR FUTURE READERS: Technically any one of the answers is "correct" (I tried them). They all are applicable to a slightly different situation. Ben's answer is good for getting a result in a single step if you have a fixed format. My own answer works to break down a string across multiple steps giving access to the data each step (useful for performing multiple operations) whilst still being able to deal with varying formats. Andras' answer is useful for getting it done in one step providing a result instantly whilst still being able to deal with varying formats.
Have a look at the sscanf documentation here, if you want a set of floats from a string you need to specify the format. Here is an example from this page similar to yours:
tempString = '78°F 72°F 64°F 66°F 49°F';
degrees = char(176);
tempNumeric = sscanf(tempString, ['%d' degrees 'F'])'
tempNumeric =
78 72 64 66 49
In your specific case you could try:
val = sscanf(rangedata,'%fHz - %fHz')
Ben's answer is the correct answer in the case of a fixed format. However, in my case, my format changes. Therefore my proposed solution (tested and verified) is to use strtokto "break-down" the string into two separate strings, making it much more manageable and easier to parse. Code below. Thoughts?
% --- Executes when selected cell(s) is changed in existingAnnotationsTable.
function existingAnnotationsTable_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to existingAnnotationsTable (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
% AE = handles.UserData;
Indices = eventdata.Indices;
% Determine if column is column of interest
if Indices(2) == 4
rangeData = handles.existingAnnotationsTable.Data;
rangeData = rangeData{Indices(1), Indices(2)};
[dataStart, dataRemain] = strtok(rangeData);
% In this case, since there will be just a '-' as the token...
% ...we don't care about the token and only save the remainder.
[~, dataEnd] = strtok(dataRemain);
dataStart = sscanf(dataStart, '%f')
dataEnd = sscanf(dataEnd, '%f')
else
end
Motivated by my own comment about using regexp before sscanf, here's a solution which only uses the former:
str=regexp(data,'(?<dataStart>[\d\.]+)[^\d\.]+(?<dataEnd>[\d\.]+)','names');
after which str will have the fields dataStart and dataEnd as strings (so you'll need num2str afterwards). Note that this only works for simple floating point numbers (but of course the regexp could be further complicated ad nauseam if necessary). The upside is that it can be adapted to more tricky input strings, for instance the above will treat any number and kind of non-numeric (and non-dot) text between the first two numbers. The downside is regexp.

How to read multiple lines by individual in an edit text with a value of Max=5 in Matlab?

I have in my gui an edit text field that accepts multiple lines with a Max value of 5, and i can't find a way to display a matrix with the input values...something like this:
m=[m(1) m(2) m(3) m(4) m(5)];
set(handles.show,'string',m)
how can i store the values in the calculate callback..every time i run this, it brings me an error..
function masa_Callback(hObject, eventdata, handles)
% hObject handle to masa (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%h_edit is the handle to the edit box
m=str2double(get(hObject,'String'));
function calculate_Callback(hObject, eventdata, handles)
% hObject handle to agregarm (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
f = str2double(get(h_fuerza,'string')); %h_edit is the handle to the edit box
m = str2double(get(h_masa,'string')); %h_edit is the handle to the edit box
[row, column] = size(m);
for i = 1:row
eval(m{i,:}) %evaluate each line as in MATLAB command prompt
end
I have the masa_callback,rigidez_callback and fuerza_callback i try to read the user input in the edit text box...so i want to pass those values to the calculate_callback as an array to perform certain operations according to the value of n...the error that i am getting is that when for example n=2, i add two values in the masa_callback column and fuerza_callback and 3 values in the rigidez_callback, those values are passed to the case n==2, and when my program tries to display for example the matrix m, it displays all the values i enter together in the spaces of m(1) and m(2)...i want to put only each separated value, not joined together!...How can i fix this,, i believe that is whith an array and a loop but i dont know how, and how to pass the array values to the equation to perform operations(as numbers) and display it as string
To fix the problem with the input (assuming you have your data in some cell array, and that handles.show refers to a text box), use strvcat:
someCellArray = {'a','b'};
m = strvcat(someCellArray{:});
set(handles.show,'string',m)
Your problem stems from the line
m = str2double(get(h_masa,'string'));
You do not want to convert the string to double.
Since the String property actually returns a multiline string, you have to modify your code like this:
m = get(h_masa,'String');
nRows = size(m,1);
for iRow = 1:nRows
eval(m(i,:));
end