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.

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

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.
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
Currently now at the moment we are using the code to implement eletrical impedance tomography where data is collected using the inputs from the Arduino but at this moment no inputs are connected. Do you think perhaps this particular line ( A(i,s) = fscanf(Arduino,'%f');) returns nothing since the input is not connected ?
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.
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.)
#define s0 2
#define s1 3
#define s2 4
#define s3 5
#define s4 6
#define s5 7
#define s6 8
#define s7 9
#define r0 10
#define r1 11
#define r2 12
#define r3 13
#define e0 14
#define e1 15
#define e2 16
int c=0;
void setup() {
// Set the digital output pins as MUX selector. current injection:s. voltage control: r. MSB:s3,s7 LSB: s0,s4
pinMode(e0,OUTPUT);
pinMode(e1,OUTPUT);
pinMode(e2,OUTPUT);
pinMode(s0,OUTPUT);
pinMode(s1,OUTPUT);
pinMode(s2,OUTPUT);
pinMode(s3,OUTPUT);
pinMode(s4,OUTPUT);
pinMode(s5,OUTPUT);
pinMode(s6,OUTPUT);
pinMode(s7,OUTPUT);
pinMode(r0,OUTPUT);
pinMode(r1,OUTPUT);
pinMode(r2,OUTPUT);
pinMode(r3,OUTPUT);
Serial.println("LABEL,Computer Time,Time (Milli Sec.),Volt");
analogReference(INTERNAL2V56);
Serial.begin(9600);
}
void loop() {
c=c+1; // MUX 1 selector will cycle from 0000 to 1111, MUX 2 selector from 0001 to 0000
int counts, countr,i=0,j=0,k;
float v=0,a[1000],mean=0;
//Variable EV1 and EV2 are the voltage of the 2 electrodes being measured, V is the difference.
digitalWrite(e0,HIGH);
digitalWrite(e1,HIGH);
digitalWrite(e2,HIGH);
for (counts=0;counts<8;counts++) // for loop that run 16 times, counts indicates the current cycle
{
digitalWrite(s7,LOW);
digitalWrite(s6,((counts+1)/4)%2);
digitalWrite(s5,((counts+1)/2)%2);
digitalWrite(s4,((counts+1))%2);
digitalWrite(s3,LOW);
digitalWrite(s2,((counts)/4)%2);
digitalWrite(s1,((counts)/2)%2);
digitalWrite(s0,((counts))%2);
delay(0);
for (countr=0;countr<8;countr++) //inner for loop to cycle through the electrodes for voltage measurement,
{
digitalWrite(e2,HIGH);
if ((countr==counts)||(countr==counts+1))
{Serial.print("DATA,TIME,");
Serial.print(",");
Serial.println(0);continue;}
digitalWrite(r3,LOW);
digitalWrite(r2,((countr)/4)%2);
digitalWrite(r1,((countr)/2)%2);
digitalWrite(r0,((countr))%2);
delay(0);
mean=0;
for (i=0;i<400;i++)
{a[i]=analogRead(A1);
mean=mean+a[i]*a[i];
if (a[i]>v) v=a[i];
v=(v*5.000/1024.000);
}
mean=mean/400.00;
mean=sqrt(mean);
mean=(mean*5.0000)/1024;
Serial.print("DATA,TIME,");
Serial.print(",");
Serial.println(mean,7);
}
}delay(1000000);
}
This is the code from the arduino
This is the output values from the serial monitor.
At the moment we are unsure if the values are sending to matlab. Could you define more on the last 2 paragraph. Thank you
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.
function varargout = EITAPP(varargin)
% EITAPP MATLAB code for EITAPP.fig
% EITAPP, by itself, creates a new EITAPP or raises the existing
% singleton*.
%
% H = EITAPP returns the handle to a new EITAPP or the handle to
% the existing singleton*.
%
% EITAPP('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EITAPP.M with the given input arguments.
%
% EITAPP('Property','Value',...) creates a new EITAPP or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before EITAPP_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to EITAPP_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help EITAPP
% Last Modified by GUIDE v2.5 18-Jul-2017 06:15:23
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @EITAPP_OpeningFcn, ...
'gui_OutputFcn', @EITAPP_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
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 EITAPP is made visible.
function EITAPP_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 EITAPP (see VARARGIN)
% Choose default command line output for EITAPP
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes EITAPP wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(handles.togglebutton5,'Enable','Off');
set(handles.pushbutton4,'Enable','Off');
cd C:\Users\hal19\Documents\MATLAB\eidors; %(for window)Change current folder to allow access to eidors
%%cd /Users/lekjianhong/Documents/MATLAB/supportpackages/eidors-v3.9/eidors;(for mac)
%%cd /Users/lekjianhong/Documents/MATLAB/supportpackages/eidors-v3.9/eidors;%!!!!!need to change the directory of the files!!!!!!
run startup;
% --- Outputs from this function are returned to the command line.
function varargout = EITAPP_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.
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)hom
set(handles.togglebutton5,'Enable','On');
set(handles.togglebutton6,'Enable','Off');
set(handles.pushbutton1,'Enable','Off');
axes(handles.axes2);
clear
clc
%model=mk_common_model('d2c2',16);
%stim = mk_stim_patterns(16,1,'{ad}','{ad}',{},1);
model = mk_common_model('d2c2',8);
stim = mk_stim_patterns(8,1,'{ad}','{ad}',{},1);
model.fwd_model.stimulation = stim;
%Imghom = mk_image(model);
%H = calc_jacobian(Imghom);
%lambda = 0.001;
%alpha = 1;
%Nit = 100;
%model.RtR_prior = @prior_noser;
%model.hyperparameter.value = 0.08;
a=0;
runs=1;
global img;
img=1;
global true;
true=1;
global freeze;
freeze=0;
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
A = zeros(64,1);%Temp Data
B = zeros(64,1);%Temp Array use for calculation
C = zeros(40,1);%Array for Inhomogenoeus
D = zeros(40,1);%Array for Homogeneous
Arduino=serial('COM4','BaudRate',9600);%!!!!!!!!need to change the port for windows!!!!!!
fopen(Arduino);
writedata=uint16(500); %0x01F4
fwrite(Arduino,writedata,'uint16') %write datac
for s = 1:1
for i=1:64 %read 64 lines of data
%A(i,1) = fscanf(Arduino,'%f')
A(i,s) = fscanf(Arduino,'%f');
end
B(A,1) = abs(B(5,1)-B(6,1));
D(4,1) = abs(B(6,1)-B(7,1));
D(5,1) = abs(B(7,1)-B(8,1));
D(6,1) = abs(B(12,1)-B(13,1));
D(7,1) = abs(B(13,1)-B(14,1));
D(8,1) = abs(B(14,1)-B(15,1));
D(9,1) = abs(B(15,1)-B(16,1));
D(10,1) = abs(B(16,1)-B(9,1));
D(1,1) = abs(B(3,1)-B(4,1));
D(2,1) = abs(B(4,1)-B(5,1));
D(3,1);
D(11,1) = abs(B(17,1)-B(18,1));
D(12,1) = abs(B(21,1)-B(22,1));
D(13,1) = abs(B(22,1)-B(23,1));
D(14,1) = abs(B(23,1)-B(24,1));
D(15,1) = abs(B(24,1)-B(17,1));
D(16,1) = abs(B(25,1)-B(26,1));
D(17,1) = abs(B(26,1)-B(27,1));
D(18,1) = abs(B(30,1)-B(31,1));
D(19,1) = abs(B(31,1)-B(32,1));
D(20,1) = abs(B(32,1)-B(25,1));
D(21,1) = abs(B(33,1)-B(34,1));
D(22,1) = abs(B(34,1)-B(35,1));
D(23,1) = abs(B(35,1)-B(36,1));
D(24,1) = abs(B(39,1)-B(40,1));
D(25,1) = abs(B(40,1)-B(33,1));
D(26,1) = abs(B(41,1)-B(42,1));
D(27,1) = abs(B(42,1)-B(43,1));
D(28,1) = abs(B(43,1)-B(44,1));
D(29,1) = abs(B(44,1)-B(45,1));
D(30,1) = abs(B(48,1)-B(41,1));
D(31,1) = abs(B(49,1)-B(50,1));
D(32,1) = abs(B(50,1)-B(51,1));
D(33,1) = abs(B(51,1)-B(52,1));
D(34,1) = abs(B(52,1)-B(53,1));
D(35,1) = abs(B(53,1)-B(54,1));
D(36,1) = abs(B(58,1)-B(59,1));
D(37,1) = abs(B(59,1)-B(60,1));
D(38,1) = abs(B(60,1)-B(61,1));
D(39,1) = abs(B(61,1)-B(62,1));
D(40,1) = abs(B(62,1)-B(63,1));
Hom(:,s) = D;
end
disp('Homogeneous data DONE')
while true==1 %%Loop Program forever
try %%Keep trying for vector Data from ardunio file
for i=1:64 %read 64 lines of data
A(i,1) = fscanf(Arduino,'%f');
end
B = A;
C(1,1) = abs(B(3,1)-B(4,1));
C(2,1) = abs(B(4,1)-B(5,1));
C(3,1) = abs(B(5,1)-B(6,1));
C(4,1) = abs(B(6,1)-B(7,1));
C(5,1) = abs(B(7,1)-B(8,1));
C(6,1) = abs(B(12,1)-B(13,1));
C(7,1) = abs(B(13,1)-B(14,1));
C(8,1) = abs(B(14,1)-B(15,1));
C(9,1) = abs(B(15,1)-B(16,1));
C(10,1) = abs(B(16,1)-B(9,1));
C(11,1) = abs(B(17,1)-B(18,1));
C(12,1) = abs(B(21,1)-B(22,1));
C(13,1) = abs(B(22,1)-B(23,1));
C(14,1) = abs(B(23,1)-B(24,1));
C(15,1) = abs(B(24,1)-B(17,1));
C(16,1) = abs(B(25,1)-B(26,1));
C(17,1) = abs(B(26,1)-B(27,1));
C(18,1) = abs(B(30,1)-B(31,1));
C(19,1) = abs(B(31,1)-B(32,1));
C(20,1) = abs(B(32,1)-B(25,1));
C(21,1) = abs(B(33,1)-B(34,1));
C(22,1) = abs(B(34,1)-B(35,1));
C(23,1) = abs(B(35,1)-B(36,1));
C(25,1) = abs(B(39,1)-B(40,1));
C(25,1) = abs(B(40,1)-B(33,1));
C(26,1) = abs(B(41,1)-B(42,1));
C(27,1) = abs(B(42,1)-B(43,1));
C(28,1) = abs(B(43,1)-B(44,1));
C(29,1) = abs(B(44,1)-B(45,1));
C(30,1) = abs(B(48,1)-B(41,1));
C(31,1) = abs(B(49,1)-B(50,1));
C(32,1) = abs(B(50,1)-B(51,1));
C(33,1) = abs(B(51,1)-B(52,1));
C(34,1) = abs(B(52,1)-B(53,1));
C(35,1) = abs(B(53,1)-B(54,1));
C(36,1) = abs(B(58,1)-B(59,1));
C(37,1) = abs(B(59,1)-B(60,1));
C(38,1) = abs(B(60,1)-B(61,1));
C(39,1) = abs(B(61,1)-B(62,1));
C(40,1) = abs(B(62,1)-B(63,1));
%%Image=(inv_solve(mk_common_model('d2c2',16),Homogeneous,DataInHomogeneous)); %%Algorithm to solve 256 data points to reconstruct image
%D = mean(Hom,2);
Image1 = inv_solve(model,D,C); % 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;
%{
Image2 = mk_image(model); % for ista
T = 0.25*max(abs(Image.elem_data(:)));
[x,~] = ista(C-D, H, lambda, alpha, Nit, T);
Image2.elem_data = x;
Image2.calc_colours.npoints = 128;
Image2.calc_slices.filter = ones(3)/9;
%}
%{
StdHom = std(Hom,0,2); % STD along Matrix rows
Maxstd = max(StdHom);
%}
%{
Diff = Hom - repmat(Hom(:,1),1,size(Hom,2));
Maxstd = max(max(Diff));
Img0 = Image1;
Img0.elem_data(:) = 1;
stdinhom = max(C-D);
X = ['Maximum Diff data is', num2str(Maxstd), 'and Std of Inhom is', num2str(stdinhom)];
disp(X);
%}
if freeze == 0
%if stdinhom <= Maxstd
%show_slices(Img0);
%else
show_slices(Image1);
%end
%show_slices(Image2);
else
pause(0.2);
end
fclose('all'); %%Keep closing txt file as to avoid crash in program
pause(0.2); %%Buffer time for program to process each individual(256 data points) for image reconstruction
catch %%Catch vector data invalidity from arduino file
pause(0.1);
% message = showinfowindow({'Please Wait...'... %%Display Message
% 'Acquiring Data...',...
% 'Refreshing...'},'Data Acquisition');
% pause(1); %%Buffer time for program to process ardunio inputs
% close(message)
% runs=runs+1;
% if(runs==10) %%Only 10 refresh cycles total before program terminates to avoid crash
% message1=warndlg('Restart Program...Too many Refreshes...Program Terminated.');
% waitfor(message1); %%Wait for user to close message box before terminating program
% close all hidden;
% break;
% end
end
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global true
true=0;
fclose('all');
close all force;
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global img;
F = getframe(handles.axes2);
Image = frame2im(F);
%%cd C:\Users\-\Desktop\DIP_DATA
cd C:\Users\hal19\Documents\MATLAB\eidors;
imwrite( Image, sprintf('image%d.jpg', img) );
img=img+1;
cd C:\Users\hal19\Documents\MATLAB\eidors;
%%cd C:\Users\-\Documents\MATLAB\eidors-v3.9\eidors;
% --- Executes on button press in togglebutton5.
function togglebutton5_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton5
set(handles.pushbutton4,'Enable','On');
button_state = get(hObject,'Value');
global freeze;
if button_state == get(hObject,'Max')
set(handles.togglebutton5,'string','Unpause');
freeze=1;
else
set(handles.pushbutton4,'Enable','Off');
set(handles.togglebutton5,'string','Pause');
freeze=0;
end
% --- Executes on button press in togglebutton6.
function togglebutton6_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton6
set(handles.pushbutton1,'Enable','Off');
button_state = get(hObject,'Value');
global true;
if button_state == get(hObject,'Max')
set(handles.togglebutton6,'string','Stop');
true=1;
imdl= mk_common_model('j2C',16);
img_1 = mk_image(imdl);
img_1.calc_colours.npoints = 128;
img_1.calc_slices.filter = ones(3)/9;
img_2 = img_1;
load('Simulation.mat','SimulationData');
xaxis=SimulationData(:,1);
yaxis=SimulationData(:,2);
counter=1;
radius=0.1;
while true==1
try
select_fcn =@(x,y,z) (x-(xaxis(counter))).^2+(y-(yaxis(counter))).^2<radius^2;
img_2.elem_data = 1 + elem_select(img_2.fwd_model, select_fcn);
if counter > 47 && counter<100
%radius=radius-0.001;
radius=radius-0;
elseif counter>=1 && counter<48
%radius=radius+0.001;
radius=radius+0;
end
stim = mk_stim_patterns(16,1,[0,1],[0,1],{},1);
img_1.fwd_model.stimulation = stim;
homg_data=fwd_solve( img_1 );
H = calc_jacobian(img_1);
lambda = 0.001; alpha = 1; Nit = 100;
%axes(handles.axes3);
%show_fem(img_2);
%show_slices(img_2);
img_2.fwd_model.stimulation = stim;
inhomg_data=fwd_solve( img_2 );
noise_level= std(inhomg_data.meas - homg_data.meas)/10^(20/20);
inhomg_data.meas = inhomg_data.meas + noise_level* ...
randn(size(inhomg_data.meas));
axes(handles.axes2);
[x,~] = ista(inhomg_data.meas-homg_data.meas, H, lambda, alpha, Nit);
img_2.elem_data = x;
show_slices(img_2);
axes(handles.axes3);
imgrec = inv_solve(mk_common_model('d2c2',16),homg_data,inhomg_data);
imgrec.elem_data = soft(imgrec.elem_data,0.005);
imgrec.calc_colours.npoints = 128;
imgrec.calc_slices.filter = ones(3)/9;
show_slices(imgrec);
counter=counter+1;
pause(0.2);
catch
message1=warndlg({'End of Simulation!'...
'Press stop to carry on with real system'},'Simulation');
waitfor(message1);
break;
end
end
else
true=0;
set(handles.togglebutton6,'string','Simulate');
set(handles.pushbutton1,'Enable','On');
cla(handles.axes2,'reset')
set(handles.axes2,'Color','black');
cla(handles.axes3,'reset')
set(handles.axes3,'Color','black');
set(handles.axes3,'YTick',[]);
set(handles.axes3,'XTick',[]);
end
Matlab Code
#define s0 2
#define s1 3
#define s2 4
#define s3 5
#define s4 6
#define s5 7
#define s6 8
#define s7 9
#define r0 10
#define r1 11
#define r2 12
#define r3 13
#define e0 14
#define e1 15
#define e2 16
int c=0;
void setup() {
// Set the digital output pins as MUX selector. current injection:s. voltage control: r. MSB:s3,s7 LSB: s0,s4
pinMode(e0,OUTPUT);
pinMode(e1,OUTPUT);
pinMode(e2,OUTPUT);
pinMode(s0,OUTPUT);
pinMode(s1,OUTPUT);
pinMode(s2,OUTPUT);
pinMode(s3,OUTPUT);
pinMode(s4,OUTPUT);
pinMode(s5,OUTPUT);
pinMode(s6,OUTPUT);
pinMode(s7,OUTPUT);
pinMode(r0,OUTPUT);
pinMode(r1,OUTPUT);
pinMode(r2,OUTPUT);
pinMode(r3,OUTPUT);
Serial.println("LABEL,Computer Time,Time (Milli Sec.),Volt");
Serial.begin(9600);
}
void loop() {
c=c+1; // MUX 1 selector will cycle from 0000 to 1111, MUX 2 selector from 0001 to 0000
int counts, countr,i=0,j=0,k;
float v=0,a[1000],mean=0;
//Variable EV1 and EV2 are the voltage of the 2 electrodes being measured, V is the difference.
digitalWrite(e0,HIGH);
digitalWrite(e1,HIGH);
digitalWrite(e2,HIGH);
for (counts=0;counts<8;counts++) // for loop that run 16 times, counts indicates the current cycle
{
digitalWrite(s7,LOW);
digitalWrite(s6,((counts+1)/4)%2);
digitalWrite(s5,((counts+1)/2)%2);
digitalWrite(s4,((counts+1))%2);
digitalWrite(s3,LOW);
digitalWrite(s2,((counts)/4)%2);
digitalWrite(s1,((counts)/2)%2);
digitalWrite(s0,((counts))%2);
delay(0);
for (countr=0;countr<8;countr++) //inner for loop to cycle through the electrodes for voltage measurement,
{
digitalWrite(e2,HIGH);
if ((countr==counts)||(countr==(counts+1)%8))
{
Serial.println(0);continue;}
else{
digitalWrite(r3,LOW);
digitalWrite(r2,((countr)/4)%2);
digitalWrite(r1,((countr)/2)%2);
digitalWrite(r0,((countr))%2);
delay(0);
mean=0;
for (i=0;i<400;i++)
{a[i]=analogRead(A1);
mean=mean+a[i]*a[i];
if (a[i]>v) v=a[i];
v=(v*5.000/1024.000);
}
mean=mean/400.00;
mean=sqrt(mean);
mean=(mean*5.0000)/1024;
Serial.println(mean,7);
}
}
}delay(1000000);
}
Arduino Code
Serial monitor from Arduino
I have made a few changes but not too much of a difference from the previous one.
Now i am facing this issue
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.

Sign in to comment.

Answers (0)

Products

Release

R2019b

Asked:

on 28 Jul 2020

Commented:

on 16 Sep 2020

Community Treasure Hunt

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

Start Hunting!