I want to convert the cell array to struct but I dont know how to use the cell2struc command. I am new, please help me

patients = {'LastName','FirstName','Age','Weight','Diagnosis';'Johnston','John','69','230','Pulmonary Hypertension';'Lee','Kyle','34','168','Type II Diabetes';'Miller','Fionna','55','194','Congestive Heart Failure'}
patients = 4×5 cell array
{'LastName'} {'FirstName'} {'Age'} {'Weight'} {'Diagnosis' } {'Johnston'} {'John' } {'69' } {'230' } {'Pulmonary Hypertension' } {'Lee' } {'Kyle' } {'34' } {'168' } {'Type II Diabetes' } {'Miller' } {'Fionna' } {'55' } {'194' } {'Congestive Heart Failure'}
structArray = cell2struct(patients)
Error using cell2struct
Not enough input arguments.

 Accepted Answer

Hi Manh, try these codes below. After running the code open s matrix there and this will be your structure. To use cell2struct command you need give information of classes and dimensions. Good luck.
clc; clear; close all;
patients = {'Johnston','John','69','230','Pulmonary Hypertension';'Lee','Kyle','34','168','Type II
Diabetes';'Miller','Fionna','55','194','Congestive Heart Failure'};
f = {'LastName','FirstName','Age','Weight','Diagnosis'};
s = cell2struct(patients,f,2);

7 Comments

Putting CLC, CLEAR, CLOSE ALL is https://en.wikipedia.org/wiki/Cargo_cult_programming and regularly causes beginners bugs because they put it at the top of all of their code. Like this.
There is no displayed output at the start of this code, so CLC does nothing.
There is nothing to CLEAR at the start of this code.
There are no figures here, so why are you trying to CLOSE non-existent figures? And what if the user (who copies your code) has some figures open that they want to keep? Good code sticks to its functionality/purpose, and does not mess around with a completetely unrelated figures/command window/etc.
it means it has 2 dimensions. Focus on s matrix it is in 3x1 form. So it has 2 dimensions.
Sometimes you want each column of the cell array to be put into the same field name, and sometimes you want each row of the cell array to be put into the same field name. The 2 tells you which dimension to operate over to decide whether the fields should be determined by the rows or the columns (or some other dimension.)
@Stephen I am used to do it because when I work on Matlab, my workspace is always full of matrices from other scripts. So each time, I want to clean it. But I will take into account your suggestions.
If your workspace is full of matrices from other scripts... perhaps you should be writing functions instead of scripts.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!