フィルターのクリア

Use For Loop to obtains variable name and value from Table

28 ビュー (過去 30 日間)
Adrian
Adrian 2023 年 11 月 9 日
回答済み: Arun 2024 年 1 月 4 日
Hi,
I am having trouble automatically creating varaibales from the below table? at the moment doing this by coding each individual column
Any help much appreciated
  4 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 11 月 9 日
You can get the variable names from the table and use them to access the data
names = TableName.Properties.VariableNames
dpb
dpb 2023 年 11 月 9 日
編集済み: dpb 2023 年 11 月 9 日
Are the names consistent or do they change all over the place from one file to another? If they always relate to a given property but may just have a different trailing subscript such as the example file but are the same variables as far as processing given the initial name string, I often will do a name substitution to a set of convenient predefined names that are succinct but meaningful for coding. The substitution can be done with string pattern matching to ensure positions aren't moved around. Simply change the Properties.VariableNames cell array as @Dyuman Joshi notes above or use the renamevars function.
Doing it this way means you can code the application with a convenient set of variables and not worry about finding stuff dynamically at all. It can be done that way, but why make things more complicated if simpler can work?

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

回答 (1 件)

Arun
Arun 2024 年 1 月 4 日
Hi Adrian,
I understand that you want to obtain variable names and data from a table created with automatically created variable names.
This can be done using the table properties or using therenamevarsfunction. Here is a code snippet that might be useful:
%creating a sample table
% you can use dataTable = readtable('yourFile.csv','TextType','string')
% Define the number of rows and columns
numRows = 5;
numCols = 4;
% Generate random values for the table
tableData = rand(numRows, numCols);
% Create a table with column names
columnNames = {'Column1', 'Column2', 'Column3', 'Column4'};
dataTable = array2table(tableData, 'VariableNames', columnNames);
%solution to the issue:
%using table properties:
names = dataTable.Properties.VariableNames;
disp(dataTable.(names{1}));
0.9053 0.4372 0.1485 0.6844 0.7502
%Solution 2:
%Using renamevars function
allVars = 1:width(dataTable);
newNames = append("Reading",string(allVars));
dataTable = renamevars(dataTable,allVars,newNames);
disp(dataTable);
Reading1 Reading2 Reading3 Reading4 ________ ________ ________ ________ 0.90528 0.94104 0.29029 0.58789 0.43721 0.95657 0.34875 0.86429 0.14846 0.6348 0.55673 0.29821 0.68438 0.054584 0.77649 0.068242 0.75015 0.041656 0.32132 0.63615
disp(dataTable.Reading1); % here we can see that, data is accessed using variables defined by us.
0.9053 0.4372 0.1485 0.6844 0.7502
For more information regarding "renamevars" function please refer the shared MATLAB Documentation link:
I hope this helps.

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by