Solved

creating variables with colheaders after Matlab importdata

Posted on 2010-09-18
2
1,526 Views
Last Modified: 2016-03-02
Hi,

After importing a tab delimited file into matlab using the importdata function i get my headers in one cell array (textdata or colheaders) and the numerical data in data matrix.

I want to create variables from the first four letters of  each column header with the corresponding data in the data matrix. Is is possible. Please help!

Thanks
Salamay
0
Comment
Question by:salamay
2 Comments
 
LVL 1

Author Comment

by:salamay
ID: 33709367
this is how i did it and it worked

check this link for more info http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
varnames = colheaders;
for i=1:length(colheaders)
    s.(varnames{i})= data(:,i);
end

Open in new window

0
 
LVL 15

Accepted Solution

by:
yuk99 earned 300 total points
ID: 33711134
Since you want to use only first 4 letters I modified the salamay's code.
% replace invalid characters by underscore
varnames = regexprep(colheaders,'\W','_');
% convert cellarrays to char
varnames = char(varnames);
% get first 4 letters
varnames = varnames(:,1:4);
% create structure with dynamic fields
for k=1:size(varnames,1)
    % strtrim to remove leading and trailing spaces
    S.(strtrim(varnames(k,:))) = data(:,k);
end

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now