Issues with tables and writetable from data passed from uitable
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.
採用された回答
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.
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 件のコメント
Jason
2025 年 1 月 29 日
Thanks Chris, this is weird. Any idea why my column headings are not saving to file for me, are there any further checks I can do to help debug.
Also, I did notice when you reopend the file using readtable, the custom properties had been lost - is that correct?
Yes. They are properties of the data type in MATLAB and are last when you save it as a text file.
I tested outside app designer. Try that. Can you save a table with headers when working in a script?
Jason
2025 年 1 月 29 日
Ah thats a shame - is there any format where the properties won't be lost?
It seems writetable is more trouble than its worth - is that the only way to save data in a uitable and include the column headings? (amd be read back again)
"Can you save a table with headers when working in a script?"
Surely it should work in Appdesigner, or say in the document that it can't.
This is all my code.
data=T.Data; % is my uitable
class(data) % Should be double
try
[file,folder]=uiputfile({'*.csv','csv Files'},'Save Data',fullfile(app.startfolder,'TableData'));
catch
[file,folder]=uiputfile({'*.csv','csv Files'},'Save Data','C:\');
end
app.startfolder=folder;
savepath=fullfile(folder,file);
%vn=data.ColumnName
%C = array2table(data,'VariableNames',vn)
C=array2table(data);
T.ColumnName
C.Properties.VariableNames=T.ColumnName;
C.Properties
writetable(C,savepath,'WriteVariableNames', true);
C.Properties definetly says the variable names ar epresent, but there just not saving the file.
In the command window, I have this
C =
200×6 table
Idx Ypos(mm) sep1 sep2 <sep> rel<sep>
___ ________ ______ ______ ______ ________
1 106.2 1811.8 1867.5 1839.7 0
2 106.4 1810.9 1867.3 1839.1 0.602
3 106.5 1811.2 1867.5 1839.4 0.3165
4 106.6 1814.3 1868.6 1841.5 -1.7845
5 106.7 1813.9 1868.9 1841.4 -1.728
ans =
TableProperties with properties:
Description: ''
UserData: []
DimensionNames: {'Row' 'Variables'}
VariableNames: {'Idx' 'Ypos(mm)' 'sep1' 'sep2' '<sep>' 'rel<sep>'}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: []
RowNames: {}
CustomProperties: No custom properties are set.
Use addprop and rmprop to modify CustomProperties.
Cris LaPierre
2025 年 1 月 29 日
編集済み: Cris LaPierre
2025 年 1 月 29 日
Depends what format you need them to be in. If you just need to save the variable so you can load it later, I would recommend saving to a mat file.
There are many options for saving to a file. writetable is a newer function that makes it simple to save tables - header line and mixed data type data. It is the option I would use writetable.
EDIT: writetable works in app designer. Just for simplicity, I have only tested your code in a script.
Jason
2025 年 1 月 29 日
Thanks Chris - I was really excited about writetable!!, so my code above Definetly doesn't save the variable names to file. I guess the only difference between your test and mine is I have extracted the table directly from a uitable.
Is there anything else I can try to see why it doesn't
Even hard coding the names like this, it still doesn't
C = array2table(data,'VariableNames',{'V1', 'V2','V3', 'V4','V5', 'V6'})
Cris LaPierre
2025 年 1 月 29 日
編集済み: Cris LaPierre
2025 年 1 月 30 日
Just tested your original code in app designer. It works as expected for me.
My example also extracts the data from a uitable, though that doesn't appear to be relevant for the issue you are having.
Have you tried restarting MATLAB?
Interestingly, this does work:
T = uitable("Data",rand(10,6)*200);
T.ColumnName = {'Idx' 'YPos(mm)' 'Sep1' 'Sep2' '<Sep>' 'dpixel'};
data=T.Data;
C=array2table(data); % Create table format
C.Properties.VariableNames=T.ColumnName;
C
writetable(C,'F:\test.txt');
The only difference is I have all my code in a function where I pass in the table as an argument. Could this be the issue
function saveUITable(app,T)
data=T.Data;
try
[file,folder]=uiputfile({'*.csv','csv Files'},'Save Data',fullfile(app.startfolder,'TableData'));
catch
[file,folder]=uiputfile({'*.csv','csv Files'},'Save Data','C:\');
end
savepath=fullfile(folder,file);
C = array2table(data);
C.Properties.VariableNames=T.ColumnName;
writetable(C,savepath);
end
and I call it via
T=app.UITable;
saveUITable(app,T);
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;
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 件)
カテゴリ
ヘルプ センター および File Exchange で Text Files についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
