Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 0-by-0.
Show older comments
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Dec_05_test is made visible.
function Dec_05_test_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Dec_05_test (see VARARGIN)
% Choose default command line output for Dec_05_test
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
set(handles.togglebutton1,'Enable','On');
set(handles.pushbutton2,'Enable','On');
cd C:\path\to\eidors;
run startup;
% UIWAIT makes Dec_05_test wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Dec_05_test_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1"start".
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)
%Make model:
set(handles.togglebutton1,'Enable','On');
set(handles.pushbutton2,'Enable','On');
model = mk_common_model('d2c2',8);
stim = mk_stim_patterns(8,1,'{ad}','{ad}',{},1);
model.fwd_model.stimulation = stim;
global close_1;
close_1=1;
global freeze;
freeze=0;
global img;
img = 1;
%clear the momory:
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
%Connect arduino to MATLAB:
clear
Arduino=serial('COM3','BaudRate',115200);%need to change the baudrate;
fopen(Arduino);
writedata=uint16(500); %0x01F4
fwrite(Arduino,writedata,'uint16') %write datac
% %define the data file:
A = zeros(64,2);%homo data
B = zeros(64,1);%inhomo data
%read 64 lines of data of Homogeneous data:
% 1 means times: 1 colums of 64 vector;
%20, 64
for s=1:20
for i=1:64 %read 64 lines of data
A(i,s) = fscanf(Arduino,'%f');
end
end
%A(A==1023) = 0;
%A=nonzeros(A');
%A=reshape(A,40,5);
%A = A(:,11:15);
A = A*5/1024;
A = A(:,6:20);
hom = mean(A,2);
A = [];
save('homg_test.mat','hom')
disp('Homogeneous data DONE')
j=1;
k=1;
n=0;%the number of complete vector
while close_1==1 %%Loop Program forever
try %%Keep trying for vector Data from ardunio file
for i=1:64 %read 64 lines of data
B(i,1) = fscanf(Arduino,'%f');
%There is only one vector,to construct the image.
end
%B = nonzeros(B)
D(:,k) = B;
k=k+1;
inhom = B*5/1024;
C(:,j) =inhom;
j=j+1;
save('inhom_test.mat','inhom')
B = [];
global Image1;
Image1 = inv_solve(model,hom,inhom); % regularization
T = 0.25*max(abs(Image1.elem_data(:)));
Image1.elem_data = soft(Image1.elem_data,T);
Image1.calc_colours.npoints = 128;
Image1.calc_slices.filter = ones(3)/9;
pause(0.1);
if freeze == 0
%pause(0.2);
show_slices(Image1);
%pause(0.001);
n = n+1;
n
else
pause(0.001);
end%%Keep closing txt file as to avoid crash in program
%fclose('all');
pause(0.2);%%Buffer time for program to process each individual(256 data points) for image reconstruction
catch
pause(0.01)
end
save('inhom_data_whole','C');
save('inhom_data_raw','D');
end
Erros :
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 0-by-0.
Error in Dec_05_test>pushbutton1_Callback (line 133)
A(i,s) = fscanf(Arduino,'%f');
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Dec_05_test (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Dec_05_test('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Explanation and solution would be suffice
11 Comments
Mario Malic
on 28 Jul 2020
I don't use the Arduino, but this line returns nothing, so it means that fscanf was not successful
A(i,s) = fscanf(Arduino,'%f');
Try to initialize the connection, and do this command manually for one value only and see if it works. When you run it, if variables i and s are 1, so it means that it doesn't work at all.
Muhammad Hazrul
on 28 Jul 2020
Mario Malic
on 28 Jul 2020
Edited: Mario Malic
on 28 Jul 2020
Maybe function fread suits for your case.
As I saw, you wrote into file uint16 values, and you are trying to read float value. I am not sure if it is going to work.
A(i,s) = fscanf(Arduino,'%x'); % Try this
Muhammad Hazrul
on 30 Jul 2020
Walter Roberson
on 30 Jul 2020
Are your lines
Arduino=serial('COM3','BaudRate',115200);%need to change the baudrate;
fopen(Arduino);
working, even with nothing connected? After the fopen() if you display Arduino, then does it say the port is connected?
If it cannot connect to a port so the fopen() fails, then any attempt to read from the object would fail immediately.
If the fopen() somehow succeeds (port exists but has nothing talking to it) then every attempt to read from the port would time out, in which case the fscanf() and the like would return empty, and emptiness cannot be assigned to a location.
Muhammad Hazrul
on 30 Jul 2020
Walter Roberson
on 30 Jul 2020
I would call that "connected" ;-)
I notice that you are accessing the COM port using serial() instead of using arduino() . That can be appropriate, provided that you have loaded appropriate code into the arduino already to send data to the port.
Under what conditions does the code you put into the arduino start sending numbers back to MATLAB ? If nothing is being sent, then the fscanf() would time out.
(Also, make sure that you send a newline when you send the data back; if you are not sending back much data, and you are not ending newlines, then MATLAB would wait until timeout or until the input buffer filled up.)
Muhammad Hazrul
on 15 Sep 2020
Walter Roberson
on 15 Sep 2020
Arduino=serial('COM3','BaudRate',115200);%need to change the baudrate;
Serial.begin(9600);
Different baud rates. If you were using a true serial port, then that would be a serious problem. If you are using serial over USB, then I am not sure it makes any difference as USB does not really have a comparable rate.
Muhammad Hazrul
on 16 Sep 2020
Walter Roberson
on 16 Sep 2020
https://www.mathworks.com/help/matlab/ref/serial.fscanf.html#buiolzb-10
fscanf is not certain to return exactly one value. It reads until terminator and so can return multiple values or no values. In case of timeout it returns the values so far, which might be no values.
You should assign the result of the fscanf to a variable and test the size of the variable instead of just assigning into array.
Answers (0)
Categories
Find more on Use COM Objects in MATLAB 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!



