hey assistent I have this…
- User
hey assistent I have this xlswrite(fullfile(nomRepertoire,[nomFile,'',tool{iTool},'.xlsx']),export{1,i},[strrep([data.vehicle(iVehicle).Model],' / ','-') '' char(Mode(l)) '_' num2str(Vh_speed(i)) 'kph'] ); I need to change the xlswrite function.
- Assistant
The
xlswritefunction is part of MATLAB and is used to write data to Excel files. However, it has been deprecated, and MATLAB now recommends usingwritematrix,writetable, orwritecelldepending on the type of data you are dealing with. Here’s how you can update your code using these functions, assumingexport{1,i}is a cell array:matlab% Define the file name fileName = fullfile(nomRepertoire, [nomFile, '_', tool{iTool}, '.xlsx']); % Define the sheet name sheetName = [strrep([data.vehicle(iVehicle).Model], ' / ', '-') '_' char(Mode(l)) '_' num2str(Vh_speed(i)) 'kph']; % Assume export{1,i} is a cell array and use writecell writecell(export{1,i}, fileName, 'Sheet', sheetName);Here's a brief explanation for replacement options:
- User
ok first modify the xlswrite function break it apart so it isn't so long and I can understand something
- Assistant
Absolutely! Breaking down the line can make it more understandable. Here’s how you can deconstruct the command into more manageable parts:
matlab% Construct the file name fileBaseName = [nomFile, '_', tool{iTool}, '.xlsx']; fileName = fullfile(nomRepertoire, fileBaseName); % Construct the Vehicle Model name by replacing ' / ' with '-' vehicleModel = strrep(data.vehicle(iVehicle).Model, ' / ', '-'); % Construct the sheet name modeName = char(Mode(l)); speedString = [num2str(Vh_speed(i)), 'kph']; sheetName = [vehicleModel, '_', modeName, '_', speedString]; % Write the data to the Excel file xlswrite(fileName, export{1,i}, sheetName); - User
do the same writecell logic for xlswrite(fullfile(nomRepertoire,[nomFile,'',tool{iTool},'.xlsx']),... export{1,i},[strrep([data.vehicle(iVehicle).Model(1:19),num2str(iVehicle)],' / ','-') '' modechar(1:3) '_' num2str(Vh_speed(i)) 'kph']);
- Assistant
Certainly! Let's break this down for
writecellin a structured way, assuming thatexport{1,i}is a cell array that you want to write to an Excel file.Here's how the revised code can look:
matlab - User
write it like this fileName = fullfile(nomRepertoire, [nomFile, '_', tool{iTool}, '.xlsx']);
% Define the sheet name sheetName = [strrep([data.vehicle(iVehicle).Model], ' / ', '-') '' char(Mode(l)) '' num2str(Vh_speed(i)) 'kph'];
% Assume export{1,i} is a cell array and use writecell writecell(export{1,i}, fileName, 'Sheet', sheetName) I don't need to have 10000 variables
- Assistant
Got it! Here's a more concise version using
writecell, without creating too many intermediate variables:matlab% Construct the file name fileName = fullfile(nomRepertoire, [nomFile, '_', tool{iTool}, '.xlsx']); % Construct the sheet name sheetName = [strrep([data.vehicle(iVehicle).Model(1:19), num2str(iVehicle)], ' / ', '-') '_' modechar(1:3) '_' num2str(Vh_speed(i)) 'kph']; % Write the data to the Excel file writecell(export{1, i}, fileName, 'Sheet', sheetName);This script directly constructs
fileNameandsheetNamewith the necessary modifications, maintaining clarity without introducing unnecessary variables. Thewritecellfunction then writesexport{1, i}to the specified file and sheet.