Array with name, number, date in the same column and I need help to split them

조회 수: 8 (최근 30일)
Victor Szenes
Victor Szenes 2021년 3월 24일
답변: Arjun 2025년 6월 10일
Hello community,
I am new to matlab and I imported an excel file which includes data of string, number and dates (all data are in the same column) and I want to split it into 3 columns.
Information of the data:
string=name
number=ID
date=birthdate
example:
Person XYZ
12345678
01.01.1990
Person ABC
19876543
01.11.1995
and I want to "transpose" the data like that:
column 1 column 2 column 3
Person XY 12345678 01.01.1990
Person ABC 19876543 01.11.1995
I think its not that difficult to solve this problem, I already tried so solve it with a for loop and if statement but I have some problems to get the right syntax.
Thanks in advance!
stay safe!!! :-)

답변 (1개)

Arjun
Arjun 2025년 6월 10일
I understand that you have an Excel file where all the data is stored in a single column, and the entries follow a repeating pattern: a name, followed by an ID number, and then a date. You want to restructure this data into a table format where each row represents one complete record, with separate columns for the name, ID, and birthdate.
You can use MATLAB’s "readcell" function to import the data from the Excel file into a cell array. Since the data is organized in a repeating pattern (e.g., name, ID, date), you can calculate the number of rows required by dividing the total number of elements in the cell array by 3. Then, use the "reshape" function to reorganize the data into a matrix with three columns—one for each data type. Finally, for better readability and further processing, you can convert the reshaped cell array into a table using the "cell2table" function.
Kindly refer to the code section below for the implementation of the above process:
% Read the data from Excel, specify your file name
rawData = readcell('dummy_data.xlsx');
% Determine number of rows
numRows = length(rawData) / 3;
% Reshape the data and then transpose
reshapedData = reshape(rawData, 3, numRows)';
% Convert to table for better readability
T = cell2table(reshapedData, 'VariableNames', {'Name', 'ID', 'date'});
% Display the result
disp(T);
You can refer to the official documentation for each of the MATLAB functions mentioned using the links provided below:
I hope this helps!

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by