Issues with tables and writetable from data passed from uitable

72 ビュー (過去 30 日間)
Jason
Jason 2025 年 1 月 29 日 11:14
編集済み: Cris LaPierre 2025 年 1 月 30 日 16:04
Hello, I have an array in a uitable that I want to save to a csv file as well as the column headings. It seems out of the write options available (writecell,writematrix, writetable) - it is only writetable that allows this
Writetable appears to work, but when I open the CSV file it hasn't and I can't understand why
This is my code:
T=app.UITable;
data=T.Data;
C=array2table(data); % Create table format
T.ColumnName
C.Properties.VariableNames=T.ColumnName;
C.Properties.Description='Scan Profile';
C = addprop(C,{'ScanStart','ScanTime'},{'table','table'});
C.Properties.CustomProperties.ScanStart = app.scannow;
C.Properties.CustomProperties.ScanTime = app.scantime;
C.Properties
% C.Properties.VariableNames
writetable(C,savepath);
ReportMessage(app,' Data Saved Using Writetable');
And in the command window I get this, indicating all is O.K
ans =
6×1 cell array
{'Idx' }
{'YPos(mm)'}
{'Sep1' }
{'Sep2' }
{'<Sep>' }
{'dpixel' }
ans =
TableProperties with properties:
Description: 'Surface Profile'
UserData: []
DimensionNames: {'Row' 'Variables'}
VariableNames: {'Idx' 'YPos(mm)' 'Sep1' 'Sep2' '<Sep>' 'dpixel'}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: []
RowNames: {}
Custom Properties (access using t.Properties.CustomProperties.<name>):
ScanStart: "29-Jan-2025 09:41:35"
ScanTime: '174.2796'
But then the file looks like this:
So no headings.
And When i also try to load via readtable - all of the description and custom propeerrties are not present
ans =
TableProperties with properties:
Description: ''
UserData: []
DimensionNames: {'Row' 'Variables'}
VariableNames: {'Var1' 'Var2' 'Var3' 'Var4' 'Var5' 'Var6'}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: []
RowNames: {}
CustomProperties: No custom properties are set.
Use addprop and rmprop to modify CustomProperties.

採用された回答

Cris LaPierre
Cris LaPierre 2025 年 1 月 29 日 13:36
When I use your code, it saves a text file with headers.
T = uitable("Data",rand(10,6)*200);
T.ColumnName = {'Idx' 'YPos(mm)' 'Sep1' 'Sep2' '<Sep>' 'dpixel'};
scannow = datetime('now');
scantime = 174.2796;
data=T.Data
data = 10×6
171.5249 59.8213 55.1595 9.8443 66.3039 91.6933 114.4676 199.7349 22.7675 4.9993 165.4376 100.5209 180.9859 6.9121 160.3118 4.1988 114.8329 172.3114 24.7842 77.0931 182.3518 40.8967 193.9196 157.5548 114.6593 20.5584 104.9205 134.5695 85.3426 13.2894 103.0309 146.9288 103.2514 67.8677 68.0959 190.1515 163.0010 113.4245 123.8324 124.5776 45.5248 8.1975 169.9012 72.2889 26.8094 98.0357 163.0888 77.1396 50.7894 157.4705 197.9429 110.9397 36.0781 35.7004 166.0988 97.1575 102.6416 161.2328 192.2395 56.0918
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C=array2table(data); % Create table format
T.ColumnName
ans = 6x1 cell array
{'Idx' } {'YPos(mm)'} {'Sep1' } {'Sep2' } {'<Sep>' } {'dpixel' }
C.Properties.VariableNames=T.ColumnName;
C.Properties.Description='Scan Profile';
C = addprop(C,{'ScanStart','ScanTime'},{'table','table'});
C.Properties.CustomProperties.ScanStart = scannow;
C.Properties.CustomProperties.ScanTime = scantime;
C.Properties
ans =
TableProperties with properties: Description: 'Scan Profile' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'Idx' 'YPos(mm)' 'Sep1' 'Sep2' '<Sep>' 'dpixel'} VariableTypes: ["double" "double" "double" "double" "double" "double"] VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} Custom Properties (access using t.Properties.CustomProperties.<name>): ScanStart: 29-Jan-2025 13:33:11 ScanTime: 174.2796
writetable(C);
However, only the text your see in the text file is saved, This means that most of the properties are lost when saving the data.
type('C.txt')
Idx,YPos(mm),Sep1,Sep2,<Sep>,dpixel 171.524925850556,59.8212564682293,55.1594713709911,9.84431199425133,66.3038601982844,91.6933173529022 114.467597476103,199.73486798322,22.7675124013326,4.99931215942839,165.437579063185,100.520894309323 180.985910692632,6.91213151523096,160.311776186681,4.19875762026298,114.832891340523,172.311408941264 24.7842180086756,77.0930680542322,182.351778449844,40.896744331036,193.919637971832,157.554847634127 114.659265530011,20.5583721104746,104.92049394351,134.569504533232,85.3425691967115,13.2894332691884 103.030871281363,146.928813035113,103.251375027387,67.8676920607874,68.0959064306495,190.151456549757 163.000970776348,113.424463285256,123.83239681839,124.577605791821,45.5247702437855,8.19750164544635 169.901197946299,72.2889152421108,26.8094140819827,98.0356640710453,163.0888367296,77.1395638567696 50.7893869535271,157.470487438356,197.942930322987,110.939718850646,36.0780921092393,35.700440729591 166.09876104556,97.1574559567013,102.641631726613,161.232808197506,192.239545789888,56.0917950510978
B = readtable('C.txt')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
B = 10x6 table
Idx YPos_mm_ Sep1 Sep2 x_Sep_ dpixel ______ ________ ______ ______ ______ ______ 171.52 59.821 55.159 9.8443 66.304 91.693 114.47 199.73 22.768 4.9993 165.44 100.52 180.99 6.9121 160.31 4.1988 114.83 172.31 24.784 77.093 182.35 40.897 193.92 157.55 114.66 20.558 104.92 134.57 85.343 13.289 103.03 146.93 103.25 67.868 68.096 190.15 163 113.42 123.83 124.58 45.525 8.1975 169.9 72.289 26.809 98.036 163.09 77.14 50.789 157.47 197.94 110.94 36.078 35.7 166.1 97.157 102.64 161.23 192.24 56.092
B.Properties
ans =
TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'Idx' 'YPos_mm_' 'Sep1' 'Sep2' 'x_Sep_' 'dpixel'} VariableTypes: ["double" "double" "double" "double" "double" "double"] VariableDescriptions: {'Idx' 'YPos(mm)' 'Sep1' 'Sep2' '<Sep>' 'dpixel'} VariableUnits: {} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
  11 件のコメント
Cris LaPierre
Cris LaPierre 2025 年 1 月 29 日 20:21
Since your uitable is already an app object, rather than pass in T, try updating your code to
data = app.UITable.Data
and
C.Properties.VariableNames=app.UItable.ColumnName;
Jason
Jason 2025 年 1 月 29 日 20:38
編集済み: Jason 2025 年 1 月 29 日 21:14
Problem solved!!!
I had a writecell command later in my code I forgot to comment out when I started using writetable. So it was working, just being over written with the write cell!
Thanks for all your help

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by