Real time data write to excel

12 Ansichten (letzte 30 Tage)
UMAIR RASOOL
UMAIR RASOOL am 26 Mär. 2021
Beantwortet: Jaswanth am 7 Feb. 2024
hello everyone
i have a question that i am geeting data from simulink to app desingner using the listioners call back.my question is that i am geeting one data point at each run and i wan tto wirte that into a file. but when i record it the data is writing into a single box and replaces it with each lisitoner call.....can somebody help me to write the data in file with each listioner call...
function varargout = updateApp(varargin)
persistent app_plot
%create a run time object that can return the value of the gain block's
%output and then put the value in a string.
rto1 = get_param('Analog_interface/A0','RuntimeObject');
str1 = num2str(rto1.InputPort(1).Data);
rto2 = get_param('Analog_interface/A1','RuntimeObject');
str2 = num2str(rto2.InputPort(1).Data);
rto3 = get_param('Analog_interface/A2','RuntimeObject');
str3 = num2str(rto3.InputPort(1).Data);
rto4 = get_param('Analog_interface/A3','RuntimeObject');
str4 = num2str(rto4.InputPort(1).Data);
rto5 = get_param('Analog_interface/A4','RuntimeObject');
str5 = num2str(rto5.InputPort(1).Data);
rto6 = get_param('Analog_interface/A5','RuntimeObject');
str6 = num2str(rto6.InputPort(1).Data);
rto7 = get_param('Analog_interface/A6','RuntimeObject');
str7 = num2str(rto7.InputPort(1).Data);
rto8 = get_param('Analog_interface/A7','RuntimeObject');
str8 = num2str(rto8.InputPort(1).Data);
rto9 = get_param('Analog_interface/A8','RuntimeObject');
str9 = num2str(rto9.InputPort(1).Data);
rto10 = get_param('Analog_interface/A9','RuntimeObject');
str10 = num2str(rto10.InputPort(1).Data);
%get a handle to the GUI's 'current state' window
all_tag_objects = findall(0, '-property', 'tag');
all_tags = get(all_tag_objects, 'tag');
[tf1, idx1] = ismember('A0', all_tags)
[tf2, idx2] = ismember('A1', all_tags);
[tf3, idx3] = ismember('A2', all_tags);
[tf4, idx4] = ismember('A3', all_tags);
[tf5, idx5] = ismember('A4', all_tags);
[tf6, idx6] = ismember('A5', all_tags);
[tf7, idx7] = ismember('A6', all_tags);
[tf8, idx8] = ismember('A7', all_tags);
[tf9, idx9] = ismember('A8', all_tags);
[tf10, idx10] = ismember('A9', all_tags);
XData = get_param('Analog_interface','SimulationTime');
[tf11, idx11] = ismember('UIAxes', all_tags);
if (tf11==1)
app_plot = all_tag_objects(idx11);
end
if (tf1==true)
st1 = all_tag_objects(idx1);
set(st1,'Value',str2double(str1));
YData = rto1.InputPort(1).Data;
assignin("base",'Out1',YData);
plot(app_plot,XData,YData,'.');
hold(app_plot, 'on' )
drawnow;
elseif (tf2==true)
st2 = all_tag_objects(idx2);
set(st2,'Value',str2double(str2));
YData = rto2.InputPort(1).Data;
file_write();
plot(app_plot,XData,YData,'.');
hold(app_plot, 'on' );
drawnow;
elseif (tf3==true)
st3 = all_tag_objects(idx3);
set(st3,'Value',str2double(str3));
YData = rto3.InputPort(1).Data;
plot(app_plot,XData,YData,'.');
hold(app_plot, 'on' );
drawnow;
elseif (tf4==true)
st4 = all_tag_objects(idx4);
set(st4,'Value',str2double(str4));
YData = rto4.InputPort(1).Data;
plot(app_plot,XData,YData,'.');
hold(app_plot, 'on' );
drawnow;
elseif (tf5==true)
st5 = all_tag_objects(idx5);
set(st5,'Value',str2double(str5));
YData = rto5.InputPort(1).Data;
plot(app_plot,XData,YData,'.');
hold(app_plot, 'on' );
drawnow;
elseif (tf6==true)
st6 = all_tag_objects(idx6);
set(st6,'Value',str2double(str6));
YData = rto6.InputPort(1).Data;
plot(app_plot,XData,YData,'.');
hold(app_plot, 'on' );
drawnow;
elseif (tf7==true)
st7 = all_tag_objects(idx7);
set(st7,'Value',str2double(str7));
YData = rto7.InputPort(1).Data;
plot(app_plot,XData,YData,'.');
hold(app_plot, 'on' );
drawnow;
elseif (tf8==true)
st8 = all_tag_objects(idx8);
set(st8,'Value',str2double(str8));
YData = rto8.InputPort(1).Data;
plot(app_plot,XData,YData,'.');
hold(app_plot, 'on' );
drawnow;
elseif (tf9==true)
st9 = all_tag_objects(idx9);
set(st9,'Value',str2double(str9));
YData = rto9.InputPort(1).Data;
plot(app_plot,XData,YData,'.');
hold(app_plot, 'on' );
drawnow;
elseif (tf10==true)
st10 = all_tag_objects(idx10);
set(st10,'Value',str2double(str10));
YData = rto10.InputPort(1).Data;
plot(app_plot,XData,YData,'.');
hold(app_plot, 'on' );
drawnow;
end
  4 Kommentare
Chinna
Chinna am 3 Nov. 2023
Thanks Mario Malic for the reply....
When i am oprating GUI with real time simulator with this code, the OPAL-RT is not working with the real time. Is there any other sytax for getting contineous wavefrom.
Mario Malic
Mario Malic am 3 Nov. 2023
I don't know anything about OPAL-RT

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jaswanth
Jaswanth am 7 Feb. 2024
Hi,
Based on the details you've given; it seems that you aim to append data to a file while ensuring that no existing data points are overwritten. To continuously write real-time data to an Excel file without overwriting the same cell, you need to keep track of the row index where the next data point should be written. You can do this using a 'persistent variable' that increments with each listener call. Additionally, you can use the 'writematrix' function to append data to the Excel file.
Assuming that file_write function is not defined in the code you have provided and you might want to record both the timestamp (XData) and the corresponding data value (YData) at each point in time, here's an example of how you can modify your file_write function to achieve this:
function file_write(YData, XData)
% Define the Excel file path
filename = 'real_time_data.xlsx';
% Define a persistent variable to keep track of the row index
persistent row_index;
if isempty(row_index)
row_index = 1; % Start from the first row if it's the first call
else
row_index = row_index + 1; % Increment the row index
end
% Create the data array to write, including the timestamp (XData) and value (YData)
data_to_write = [XData, YData];
% Write the data to the Excel file at the specified row index
writematrix(data_to_write, filename, 'WriteMode', 'append');
end
The above-mentioned example code will append the data to the file with each call to file_write, and the row_index will ensure that each new data point is written to a new row.
Also, Make sure to call file_write within each of your elseif blocks where you want to record the data.
I hope the information shared is useful in resolving your issue.

Community Treasure Hunt

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

Start Hunting!

Translated by