Solved

creating variables with colheaders after Matlab importdata

Posted on 2010-09-18
2
1,625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

630 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