Issue printing and saving some data in a file ID from excel in Guide

1 view (last 30 days)
Hello everyone,
I am having an issue printing some data from an excel file. Basically, i would like to print out some data from an excel file and save them in a FileID.
I would like to print this:
handles.amplitude = data(:,5); The data are related to the amplitude and are on this format . ex: (1&1! )
handles.f_df = data(:,7); The data are all strings ex: A ,B...
I am having this as an error:
Unable to resolve the name handles.amplitude.
Error while evaluating TimerFcn for timer 'timer-2'
The code related to the issue is the last "paragraph" in the code.
function Hapticfinal_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 Hapticfinal (see VARARGIN)
% Choose default command line output for Hapticfinal
handles.output = hObject;
data = xlsread('excel_data.xlsx');
% randomly permute the rows of data:
data = data(randperm(size(data,1)),:);
handles.v_thickness_1 = data(:,1);
handles.v_thickness_2 = data(:,2);
handles.h_thickness_1 = data(:,3);
handles.h_thickness_2 = data(:,4);
handles.amplitude = data(:,5);
handles.v_or_h_array = data(:,6);
handles.f_df = data(:,7);
handles.exp_counter = 1;
handles.region1 = [];
% Create the Arduino serial object
handles.arduinoObj = serialport('COM3', 38400);
configureTerminator(handles.arduinoObj,'CR/LF');
%
for i=1:8
handles.message = readline(handles.arduinoObj);
disp(handles.message)
end
create_patch(handles);
function create_patch(handles)
if ishandle(handles.region1)
delete(handles.region1);
end
v_or_h = handles.v_or_h_array(handles.exp_counter);
if v_or_h == 0 % Vertical line
v_thick1 = handles.v_thickness_1(handles.exp_counter);
v_thick2 = handles.v_thickness_2(handles.exp_counter);
handles.region1 = patch( ...
'Parent',handles.axes1, ...
'XData',[v_thick1 v_thick2 v_thick2 v_thick1], ...
'YData',[-10 -10 10 10], ...
'FaceColor','red');
set(handles.axes1,'XLim',[-5 0],'YLim',[-10 10]);
else % Horizontal line
h_thick1 = handles.h_thickness_1(handles.exp_counter);
h_thick2 = handles.h_thickness_2(handles.exp_counter);
handles.region1 = patch( ...
'Parent',handles.axes1, ...
'XData',[-10 10 10 -10], ...
'YData',[h_thick1 h_thick1 h_thick2 h_thick2], ...
'FaceColor','red');
set(handles.axes1,'YLim',[0 5],'XLim',[-10 10]);
end
set(handles.axes1,'XGrid','on','YGrid','on');
axis(handles.axes1,'equal');
% Update handles structure
guidata(handles.finger,handles);
% call the button motion fcn to update the new patch's color:
finger_WindowButtonMotionFcn(handles.finger);
function timerCallback(~,~,fileID)
%fprintf(fileID,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now'));
fprintf(fileID,'(X, Y, time) = (%g, %g, %s, %s,%s)\n', get(0, 'PointerLocation'), datetime('now'),...
handles.amplitude,handels.f_df );
%fprintf('calling timer callback\n

Accepted Answer

Voss
Voss on 1 May 2022
Edited: Voss on 1 May 2022
I don't see the part of the code where the timer is created (actually the part where the timer's TimerFcn is specified, which may or may not be when the timer is created), but basically you have to pass some handle (e.g., the figure handles.finger) to the TimerFcn so that that function can get the handles structure.
For instance, if the part of the code where the timer's TimerFcn is specified looks like this:
fid = fopen('output.txt','w');
t = timer( ...
'TimerFcn',{@timerCallback,fid}, ...
);
It should be changed to this (this assumes handles is already defined in whatever function this part appears in):
fid = fopen('output.txt','w');
t = timer( ...
'TimerFcn',{@timerCallback,fid,handles.finger}, ... % pass the figure handle as well as the file handle
);
Then timerCallback would use guidata to get the figure's handles structure:
function timerCallback(~,~,fileID,f)
handles = guidata(f); % get the figure's handles struct
%fprintf(fileID,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now'));
fprintf(fileID,'(X, Y, time) = (%g, %g, %s, %s,%s)\n', get(0, 'PointerLocation'), datetime('now'),...
handles.amplitude,handles.f_df);%handels.f_df );
%fprintf('calling timer callback\n
Doing that will allow handles to be used by the timer.
[One thing not directly related to that is, you mention you want to write to an excel file. If you mean an xlsx file or similar, you wouldn't use fopen/fprintf. (If it's a csv file then fopen/fprintf is ok.) For xlsx or similar, you could use writematrix/writecell/writetable (or xlswrite). However, in this case, based on the data being written (i.e., '(X, Y, time) = (%g, %g, %s, %s,%s)\n'), I'd say a text file is appropriate, in which case fopen/fprintf is fine.]
  16 Comments
Voss
Voss on 2 May 2022
I think that would be:
writeline(handles.arduinoObj, handles.amplitude{handles.exp_counter})
because (if I recall correctly) handles.exp_counter is the index of the current row of data (i.e., handles.exp_counter is the thing that's incremented by 1 in Next_button_Callback).
I'm glad it's working!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots 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!