How to use cell value as a variable or How to convert cell to double?

6 ビュー (過去 30 日間)
Ankush Aher
Ankush Aher 2025 年 4 月 8 日
コメント済み: Stephen23 2025 年 4 月 9 日
I have data in excel, I am reading that data in MATLAB. I want to use text in excel as a variable name to assign fixed set of data values which also in same excel. but I am facing the problem as the text from excel has class cell.
So, how can I use cell values (Time, Temperatue....etc) as a variable in MATLAB?

採用された回答

Manikanta Aditya
Manikanta Aditya 2025 年 4 月 8 日
Follow the below steps:
  1. Read Data from Excel: Use readtable or xlsread to read data from Excel into MATLAB.
  2. Extract Text and Data: Extract the text and data from the table or cell array.
  3. Convert Text to Variable Names: Use eval or dynamic field names in a structure to assign data to variables.
% Step 1: Read data from Excel
data = readtable('yourfile.xlsx');
% Step 2: Extract text and data
variableNames = data.Properties.VariableNames;
dataValues = table2array(data);
% Step 3: Convert text to variable names and assign data
for i = 1:length(variableNames)
eval([variableNames{i} ' = dataValues(:, i);']);
end
% Example usage
disp(Time); % Assuming 'Time' is one of the column headers in your Excel file
disp(Temperature); % Assuming 'Temperature' is another column header
This code reads the data from an Excel file, extracts the column headers as variable names, and assigns the corresponding data to these variables. Make sure to replace 'yourfile.xlsx' with the actual name of your Excel file.
I hope this helps.
  5 件のコメント
Walter Roberson
Walter Roberson 2025 年 4 月 8 日
Using readtable() is certainly the recommended approach here!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by