Solved

creating variables with colheaders after Matlab importdata

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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 163
wordsFront challenge 8 89
groupSumClump challenge 9 115
base64 decode encode 12 134
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

810 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