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

8 views (last 30 days)
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?

Accepted Answer

Manikanta Aditya
Manikanta Aditya on 8 Apr 2025
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 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!