Content of a uitable is not being saved - matlab

I'm trying to write a GUI for several purposes, that looks like this,
one of them is create a matrix based on user input via a uitable, so in the untitled_OpeningFcn it was predefined the size and enabled the cell edition
set(handles.uitable1,'Data',cell(2,3));
set(handles.uitable1,'ColumnEditable',true(1,3))
inside the pushbutton2_Callback, I try to read the data written in the table, store it and display in a static text field:
value=get(handles.uitable1,'Data');
value=str2double(value);
set(handles.text2,'String',num2str(value));
But when it is running the text field says "NaN", so the data is not being saved.
What else it is needed?
UPDATE
Thanks by the aswer it does not give the expected results
When using the original code it gives the next output
and when using the suggeste mat2str I got this

I believe the reason for your problem is that the line
value = get(handles.uitable1,'Data');
doesn't result in anything that is convertible to a number.
Consider the following example:
hF = uifigure(); % works the same way with `figure()`.
hT = uitable(hF,'Data',[1 2 3; 4 5 6]);
Then, consider the following:
>> hT.Data
ans =
1 2 3
4 5 6
>> class(hT.Data)
ans =
'double'
>> str2double(hT.Data)
ans =
NaN
>> mat2str(hT.Data)
ans =
'[1 2 3;4 5 6]'
In conclusion - what you need is likely mat2str.

Related

Importing Data from a .txt file into Matlab

First of all thank you for reading my question.
I am trying to import data from a file with following format into Matlab:
#Text
#Text: Number
...
#Text: Number
Set1:
1 2
3 4
Set2:
5 6
7 8
...
I would like to get those numbers into two matrices of the form:
(1 5
3 7)
and
(2 6
4 8)
I started by only building the first of those two matrices.
Winkel = 15;
xp = 30;
M = readtable('Ebene_1.txt')
M([1:4],:) = [];
M(:,3) = [];
for i=0:Winkel-1
A = table2array(M((2+i*31:31+i*31),1))
end
But this solution only gave me cell arrays which I could not transform into normal vectors.
I also tried to use the importdata command, but could not find a way to get this to work either. I know there are many other questions similar to mine, but I could not find one where all the data were in a single column. Also, there are many Matlab-commands for importing data into Matlab and I am not sure which would be the best.
First time asking such a question online so feel free to ask me for more details.
You can import the data you provided in your sample using readtable, however because of the format of your file you will need to tweak the function a bit.
You can use detectImportOptions to tell the function how to import the data.
%Detect import options for your text file.
opts = detectImportOptions('Ebene_1.txt')
%Specify variable names for your table.
opts.VariableNames = {'Text','Number'};
%Ignore last column of your text file as it does not contain data you are interested in.
opts.ExtraColumnsRule = 'ignore';
%You can confirm that the function has successfully identified that the data is numeric by inspecting the VariableTypes property.
%opts.VariableTypes
%Read your text file with detectImportOptions.
M = readtable('Ebene_1.txt',opts)
Now that you have table M, simply apply basic Matlab operations to obtain the matrices as you specified.
%Find numerical values in Text and Number variables. Ignore NaN values.
A = M.Text(~isnan(M.Text));
B = M.Number(~isnan(M.Number));
%Build matrices.
A = [A(1:2:end)';A(2:2:end)']
B = [B(1:2:end)';B(2:2:end)']
Output:
A =
1 5
3 7
B =
2 6
4 8

Issue regarding loop to find Root mean square error in MATLAB

I have 6 different data sets from sensor and I want to find out the Root mean Square error but of the limited signal with limits means RMSE of sig_diff_lim.
I have tried to apply loop but it’s not working can anybody tell me where I am at fault.
Thanks in anticipation.
clc
drv(1)=load('a.mat');
drv(2)=load('b.mat');
drv(3)=load('c.mat');
drv(4)=load('d.mat');
drv(5)=load('e.mat');
drv(6)=load('h.mat');
for i= 1:numel(drv)
t=drv(i).T;
ref=drv(i).P;
lws=drv(i).SWA;
sig_diff(i,:) =lws(i)-ref(i);
swvel_thres=10;
vehvel=30;
SAmax=90;
sig_diff_lim(i,:)=sig_diff((lws(i)<SAmax)&(lws(i)>-SAmax)&(swav(i)<swvel_thres)&(vel(i)>vehvel));
square_error(i,:) = (sig_diff_lim(i)).^2;
mse(i,:)= mean(square_error(i));
rmse(i,:) = sqrt(mse(i));
end
rmse
mse
It seems to me that while assigning to sig_diff_lim(i,:) requires a row vector of size 1*n, the matrix that is returned by
vec=sig_diff((lws_7(i)<SAmax)&(lws_7(i)>-SAmax)&(swav(i)<swvel_thres)&(vel(i)>vehvel))
is of a different size.
Edit
First, are you sure that sig_diff((lws(i)... shouldn't be sig_diff(i,:)((lws(i).... on the assignemnt line?
Now, try element-wise multiplication (operator .* ) between sig_diff (or sig_diff(i,:)), depending on your answer to my previous point) with your logical indexing, like this:
sig_diff.*((lws(i)<... %%%% instead of sig_diff((lws(i)<...
%%% or
sig_diff(i,:).*(lws(i)<...
I think that should do the trick.
Regardless of whether or not this worked, try this example to understand what is going on with that sort of logical indexing:
>> t=[1 2 3 5 7;1 5 6 8 10];
>> t(1,:)(l<7 & l>-7)
ans =
1 2 3 5
>> t(1,:)(l<7 & l>-2)
ans =
1 2 5
Of course, in this example, t is fixed while the thresholds are changing , while in your code the matrix itself is changing. But it shows you that your indexing will return matrices of variable length.
But, with the solution I proposed above,
>> t(1,:).*(l<7 & l>-2)
>> t(1,:).*(l<7 & l>-7)
are both of size 5*1.

splitting a Matrix into column vectors and storing it in an array

My question has two parts:
Split a given matrix into its columns
These columns should be stored into an array
eg,
A = [1 3 5
3 5 7
4 5 7
6 8 9]
Now, I know the solution to the first part:
the columns are obtained via
tempCol = A(:,iter), where iter = 1:end
Regarding the second part of the problem, I would like to have (something like this, maybe a different indexing into arraySplit array), but one full column of A should be stored at a single index in splitArray:
arraySplit(1) = A(:,1)
arraySplit(2) = A(:,2)
and so on...
for the example matrix A,
arraySplit(1) should give me [ 1 3 4 6 ]'
arraySplit(2) should give me [ 3 5 5 8 ]'
I am getting the following error, when i try to assign the column vector to my array.
In an assignment A(I) = B, the number of elements in B and I must be the same.
I am doing the allocation and access of arraySplit wrongly, please help me out ...
Really it sounds like A is alread what you want--I can't imagine a scenario where you gain anything by splitting them up. But if you do, then your best bet is likely a cell array, ie.
C = cell(1,3);
for i=1:3
C{i} = A(:,i);
end
Edit: See #EitanT's comment below for a more elegant way to do this. Also accessing the vector uses the same syntax as setting it, e.g. v = C{2}; will put the second column of A into v.
In a Matlab array, each element must have the same type. In most cases, that is a float type. An your example A(:, 1) is a 4 by 1 array. If you assign it to, say, B(:, 2) then B(:, 1) must also be a 4 by 1 array.
One common error that may be biting you is that a 4 by 1 array and a 1 by 4 array are not the same thing. One is a column vector and one is a row vector. Try transposing A(:, 1) to get a 1 by 4 row array.
You could try something like the following:
A = [1 3 5;
3 5 7;
4 5 7;
6 8 9]
arraySplit = zeros(4,1,3);
for i =1:3
arraySplit(:,:,i) = A(:,i);
end
and then call arraySplit(:,:,1) to get the first vector, but that seems to be an unnecessary step, since you can readily do that by accessing the exact same values as A(:,1).

Removing consecutive duplicates from vector MATLAB

Here is my MATLAB code:
[numeric, pics] = xlsread('matrix.xls');
[r,c] = size(pics);
done = r*c;
randvecall = randsample(done, done, true);
randvec = randvecall([1,diff(randvecall)]~=0);
currk = randvec(k);
Essentially what this does is it builds an array of values from a Microsoft Excel spreadsheet. I want to have duplicates in the array, but not consecutive duplicates, so I added a line of code that removes them. When I manually enter values into randvecall and run the above code, it works perfectly. However, when I run the code as seen above, I get the following error:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> testAS_randsample at 76
randvec = randvecall([1,diff(randvecall)]~=0);
Why is this happening? For example, this works:
randvecall=[1 2 3 4 5 5 5 5 8 7 8 8];
randvec = randvecall([1,diff(randvecall)]~=0);
disp(randvec)
randvec = [1 2 3 4 5 8 7 8]
That is exactly what I want my code to do. But why does my actual code give me the horzcat error message? Can anybody help me with this? It must have something to do with the way randsample is building the randvecall array, but I can't figure out why this would give me that error message?
That seems to be a problem with how randsample(n,k,true) works: it returns a 1xk vector, while you need a kx1 vector. Transposing randvecall should do the trick.
EDIT:
Let me rephrase it in code for the general reader:
randvec = randvecall([1,diff(randvecall')]~=0);

How to use matrix from answer further?

If I write a random matrix (A) and get results:
ans = 1 2 3 4 %next row 5 6 7 8
how can I get it written in this form:
A = [1,2,3,4;5,6,7,8]; ?
(Of course I want to avoid retyping or copy-pasting it)
If I understand your question correctly, mat2str is what you are looking for. Although it won't use commas, but spaces, and overwrite ans (i.e. ans will be of type char afterwards).
Example (the second argument limits the number of digits):
>> rand(2,3); mat2str(ans,2)
ans =
[0.42 0.79 0.66;0.92 0.96 0.036]
The last answer that you calculated is saved in a special variable named ans. Simply assign that value to A.
% some calculations
[1,2,3,4;5,6,7,8]
% assign to A
A = ans;