update the column headng by readin the excel file

1 次查看(过去 30 天)
How to update the column heading of a cell vector which holds the combination?

采纳的回答

Star Strider
Star Strider 2023-7-14
编辑:Star Strider 2023-7-14
If you want the variable names, first:
T1 = readtable('YourFile.xlsx', 'VariableNamingRule','preserve')
then:
ColumnHeadings = T1.Properties.VariableNames;
then for example:
FirstHeading = ColumnHeadings{1};
to return the first heading (variable name).
EDIT — (14 Jul 2023 at 15:27)
Use the sortrows function —
% figure
% imshow(imread('2023-07-14_13h06_04.png'))
data = readtable('Book2.xlsx', 'VariableNamingRule','preserve')
data = 6×6 table
No Components DescriptioninGUI Parameters Value1 Value2 __ __________ __________________________ __________ ______ ______ 1 {'bb'} {'Length' } {'L' } 80 110 2 {'bb'} {'Width' } {'W' } NaN NaN 3 {'bb'} {'thickness' } {'T' } 1.6 2 4 {'bb'} {'height above the plate'} {'HA'} NaN NaN 5 {'bb'} {'height below the plate'} {'HB'} NaN NaN 6 {'bb'} {'edge width' } {'CE'} NaN NaN
emptyRows = all(ismissing(data),2);
data(emptyRows,:) = [];
variable = data.Parameters;
values = {};
for i = 1:size(data, 1)
values{i} = table2array(data(i, 5:end));
values{i}(all(ismissing(values{i}),2),:) = [];
end
% remove empty cell
values = values(~cellfun(@isempty,values));
combinations = combvec(values{:})';
combinations = sortrows(combinations,1); % <— ADDED
Lv = ~all(ismissing([data.Value1 data.Value2]),2); % <— ADDED
p = variable(Lv);
comitable= array2table(combinations);
comitable.Properties.VariableNames = p(1:size(combinations,2))
comitable = 4×2 table
L T ___ ___ 80 1.6 80 2 110 1.6 110 2
EDIT — (14 Jul 2023 at 15:57)
Added ‘Lv’ (and references to it) to select the correct values of ‘p’.
  17 个评论
PA
PA 2023-7-27
编辑:PA 2023-7-27
do You know how can i convert this combinations from a cell array to a vector keeping the text entries .
{'50'} {'15'} {'Name'}
{'75'} {'15'} {'Name'}
{'50'} {'10'} {'Age' }
{'75'} {'10'} {'Age' }
{'50'} {'15'} {'Age' }
{'75'} {'15'} {'Age' }
{'50'} {'10'} {'Name'}
{'75'} {'10'} {'Name'}
i tried to use str2doublr but i get this
c =
50 15 NaN
75 15 NaN
50 10 NaN
75 10 NaN
50 15 NaN
75 15 NaN
50 10 NaN
75 10 NaN
Star Strider
Star Strider 2023-7-27
Mixed numeric and text can only be combined in a cell array or a table, and a string array is another option (although I am not certain that it is much of an improvement over the cell array here, other than with respect to indexing). The cell2table function might be an option.
Perhaps one of these —
C = {{'50'} {'15'} {'Name'}
{'75'} {'15'} {'Name'}
{'50'} {'10'} {'Age' }
{'75'} {'10'} {'Age' }
{'50'} {'15'} {'Age' }
{'75'} {'15'} {'Age' }
{'50'} {'10'} {'Name'}
{'75'} {'10'} {'Name'}};
T1 = cell2table(C)
T1 = 8×3 table
C1 C2 C3 ______ ______ ________ {'50'} {'15'} {'Name'} {'75'} {'15'} {'Name'} {'50'} {'10'} {'Age' } {'75'} {'10'} {'Age' } {'50'} {'15'} {'Age' } {'75'} {'15'} {'Age' } {'50'} {'10'} {'Name'} {'75'} {'10'} {'Name'}
Text = string(C(:,3));
T2 = array2table(cellfun(@str2double,C(:,[1 2])), 'VariableNames',{'N1','N2'});
T2 = addvars(T2, Text)
T2 = 8×3 table
N1 N2 Text __ __ ______ 50 15 "Name" 75 15 "Name" 50 10 "Age" 75 10 "Age" 50 15 "Age" 75 15 "Age" 50 10 "Name" 75 10 "Name"
S = [cellfun(@str2double,C(:,[1 2])) string(C(:,3))]
S = 8×3 string array
"50" "15" "Name" "75" "15" "Name" "50" "10" "Age" "75" "10" "Age" "50" "15" "Age" "75" "15" "Age" "50" "10" "Name" "75" "10" "Name"
.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2023-7-14
You forgot to attach your workbook, and forgot to say what kind of variable you want to work with in MATLAB (table, cell array, double). Cell arrays don't have column headings unless you mean the first row of the cell array. If you have the first row be a string, and subsequent rows have numbers, then you'd be best off using a table instead of a cell array.
t = readtable(yourWorkbookFileName)
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

标签

Community Treasure Hunt

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

Start Hunting!

Translated by