Automatically add prefix to loaded sheetname, and output,

3 次查看(过去 30 天)
Hello, I have a sheetnames 'Malecontrol.xlsx' that has been reorganized to a new matrix called 'reshaped'. I want to save this new matrix but add a prefix to the original sheetnames, so it will be saved and output as 'reshaped_Malecontrol.xlsx'.
So each time I reorganize a new sheet, I don't have to change the output name. the output name can be automatically deteced.
clear all
close all
global mu
SPACE_UNITS = 'µm';
TIME_UNITS = 'min';
% Need to update names of data file, frame (f) and time interval
%% If loading in individual sheets
sheets = sheetnames('Malecontrol.xlsx');
f = 97;%update based on frame number
for i=1:size(sheets)
xy=readmatrix('Malecontrol.xlsx', 'Sheet',sheets(i), 'Range','A1:B97');
B=xy(~isnan(xy)); %takes out a defect
if size(B, 1)==f*2 % f*2
if i==1
x1=xy(:,1);
y1=xy(:,2);
else
x1=[x1; xy(:,1)];
y1=[y1; xy(:,2)];
end
end
end
VarName1=x1;
VarName2=y1;
% reshape x and y input
x=reshape(VarName1, f, []);
y=reshape(VarName2, f, []);
n = 97; % Number of repeated elements for each value
max_i = i; % Maximum value of i
cellID = repelem((1:max_i)', n, 1);
time_sequence = (0:15:1440)';
timeframe = repmat(time_sequence, i, 1);
reshaped = [cellID timeframe x1 y1];
%%
writematrix(reshaped, 'reshapedMaleControl.xlsx');

回答(1 个)

Image Analyst
Image Analyst 2024-9-10
You have a new workbook with the new name 'reshapedMaleControl.xlsx'. If your original workbook had multiple sheets, it looks like your new output workbook will have only one sheet with all the input sheets appended to each other. It doesn't look like you're "reorganizing" anything, just appending them all. Not sure what you're after. You already gave the new workbook name. Do you want multiple sheets in your output workbook? Or do you want the single sheet with the new workbook name?
Do you want the input name to be a variable and create the output name from it, like
inputBaseFileName = 'Malecontrol.xlsx';
inputFullFileName = fullfile(pwd, inputBaseFileName);
fprintf('Reading input workbook : "%s"\n', inputFullFileName);
sheets = sheetnames('Malecontrol.xlsx');
% code snipped
% Now create output workbook name
outputBaseFileName = sprintf('reshaped%s.xlsx', inputBaseFileName);
outputFillFileName = fullfile(pwd, outputBaseFileName);
fprintf('Creating output workbook : "%s"\n', outputFillFileName);
writematrix(reshaped, outputFillFileName);

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by