How Can I stored previous result with new or current result in same cell array?

2 次查看(过去 30 天)
function ImportFiredataButtonPushed(app, event)
% app.selectedFireCode = app.FireExperimentListBox.Value;
app.selected_fire_channel = app.FireChannelListBox.Value;
Fire_data_X = {};
Fire_data_Y = {};
Fire_data_Z = {};
for i = 1:length(app.All_data)
folder_path = app.All_data(i).file.folder;
for j = 1:length(app.selected_fire_channel)
if contains(folder_path, app.selected_fire_channel{j})
% Find the corresponding folder path
channel_folder_path = fullfile(folder_path, 'channel');
channel_files = dir(channel_folder_path);
for k = 1:length(channel_files)
if ~channel_files(k).isdir % Check if it's a file, not a directory
file_name = channel_files(k).name;
if endsWith(file_name, '.chn')
continue; % skip the .chn file
end
channel_filepath = fullfile(channel_folder_path, file_name);
channel_text = fileread(channel_filepath);
for m = 1:numel(app.selected_fire_list)
if contains(channel_text, app.selected_fire_list{m})
direction = app.selected_fire_list{m}(15);
if ismember(direction, ['X', 'Y', 'Z'])
% Add the data to the appropriate array based on direction
switch direction
case 'X'
Fire_data_X{end+1} = read_chn(app, channel_filepath);
case 'Y'
Fire_data_Y{end+1} = read_chn(app, channel_filepath);
case 'Z'
Fire_data_Z{end+1} = read_chn(app, channel_filepath);
end
end
end
end
end
end
end
end
end
assignin('base','Fire_data_X',Fire_data_X);
assignin('base','Fire_data_Y',Fire_data_Y);
assignin('base','Fire_data_Z',Fire_data_Z)
end
Hello,
I wrote this code to extract data from "app.selected_fire_list" which list of text file. "app.selected_fire_channel" this is the channel folder name list and each folder has multiple fire text file list. By clicking button, I am getting data it will extract data from selected fire list give me data in Fire_data_X, Fire_data_Y, and Fire_data_Z. What I want is that I have data in my workspace in Fire_data_X, Fire_data_Y, and Fire_data_Z by selecting files from "app.selected_fire_list" but If I select different channel folder name from "app.selected_fire_channel" then it give me new data in Fire_data_X, Fire_data_Y, and Fire_data_Z and old one is automatically removed from the cell. But I want both result like old and new if I select different channel folder multiple time then it just add the new result with old rsults in the cell. Could anyone know how I can do that?

回答(1 个)

Voss
Voss 2023-11-3
Instead of initializing to empty the variables you plan to populate in the base workspace, try to get them from the base workspace first so that the new elements will be appended to the existing variables.
try
% get variable from base workspace, if it exists
Fire_data_X = evalin('base','Fire_data_X');
catch
% if variable doesn't exist in base workspace, create an empty one here
Fire_data_X = {};
end
try
Fire_data_Y = evalin('base','Fire_data_Y');
catch
Fire_data_Y = {};
end
try
Fire_data_Z = evalin('base','Fire_data_Z');
catch
Fire_data_Z = {};
end
Then the rest of your code is the same: run your loops to populate those variables with new elements, and assign the results in the base workspace. At the end, the variables in the base workspace will have whatever they had before plus the new stuff.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by