Transform column data to m x n matrix
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi, I have 3 column data and want to transform it into an m x n matrix. Please see the attached file for an example.
댓글 수: 0
채택된 답변
Andrei Bobrov
2018년 10월 18일
OriginalData = readtable('Path_to_your_file\DataTransformation.xlsx',...
'Range','A3:C26',...
'ReadVariableNames',true);
[gr,G] = findgroups(OriginalData.Key2);
A = accumarray([gr,(1:numel(gr))'],OriginalData.values);
TransformedData = [[{nan};G],num2cell([OriginalData.Key1';A])];
추가 답변 (3개)
Kevin Chng
2018년 10월 17일
편집: Kevin Chng
2018년 10월 17일
Hi
The best solution is :
Drag your excel and drop it in MATLAB command window.
It will pop out a app for you to configure your excel input.
Since there are char in 2nd column, therefore I stored them in table data type instead of matrix.
opts = spreadsheetImportOptions("NumVariables", 3);
opts.Sheet = "Sheet1";
opts.DataRange = "A4:C29";
opts.VariableNames = ["Key1", "Key2", "values"];
opts.VariableTypes = ["double", "categorical", "double"];
opts = setvaropts(opts, 2, "EmptyFieldRule", "auto");
DataTransformation = readtable("DataTransformation.xlsx", opts, "UseExcel", false);
clear opts
Code above is generated through the application.
However, I saw there are other data or table in your excel sheet, if you want to import them in other table, just repeat the method what i say : Drag the excel sheet and drop it in MATLAB command window, select the range and data type you wanted to export them.
댓글 수: 2
Kevin Chng
2018년 10월 18일
Read table from Original Data, then write it to Transformed Data according to its pattern?
madhan ravi
2018년 10월 18일
편집: madhan ravi
2018년 10월 18일
[num,txt,raw]=xlsread('DataTransformation.xlsx')
[m,n]=size(num)
num(isnan(num))=[]
data=reshape(num,m,n) %m and n can be your choice but make sure m times n == number(num)
댓글 수: 3
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!