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
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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.
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.
what does the number 2 do ?
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.
@Walter Roberson that is true. using functions saves you from dirty workspace.
More Answers (0)
Categories
Find more on Data Type Identification in Help Center and File Exchange
See Also
on 3 Dec 2021
on 3 Dec 2021
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)