Solved

creating variables with colheaders after Matlab importdata

Posted on 2010-09-18
2
1,602 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

Suggested Solutions

Title # Comments Views Activity
Help with my python script 6 178
XML::LibXML and Xpath syntax How do I get attribute of sibling 2 181
wordmultiple challenge 12 170
Non-recursive backtracking, using a stack 1 312
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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