Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3810342
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:26:44+00:00 2026-05-19T15:26:44+00:00

I’m working with MATLAB for few days and I’m having difficulties to import a

  • 0

I’m working with MATLAB for few days and I’m having difficulties to import a CSV-file to a matrix.

My problem is that my CSV-file contains almost only Strings and some integer values, so that csvread() doesn’t work. csvread() only gets along with integer values.

How can I store my strings in some kind of a 2-dimensional array to have free access to each element?

Here’s a sample CSV for my needs:

04;abc;def;ghj;klm;;;;;
;;;;;Test;text;0xFF;;
;;;;;asdfhsdf;dsafdsag;0x0F0F;;

The main thing are the empty cells and the texts within the cells.
As you see, the structure may vary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:26:45+00:00Added an answer on May 19, 2026 at 3:26 pm

    For the case when you know how many columns of data there will be in your CSV file, one simple call to textscan like Amro suggests will be your best solution.

    However, if you don’t know a priori how many columns are in your file, you can use a more general approach like I did in the following function. I first used the function fgetl to read each line of the file into a cell array. Then I used the function textscan to parse each line into separate strings using a predefined field delimiter and treating the integer fields as strings for now (they can be converted to numeric values later). Here is the resulting code, placed in a function read_mixed_csv:

    function lineArray = read_mixed_csv(fileName, delimiter)
    
      fid = fopen(fileName, 'r');         % Open the file
      lineArray = cell(100, 1);           % Preallocate a cell array (ideally slightly
                                          %   larger than is needed)
      lineIndex = 1;                      % Index of cell to place the next line in
      nextLine = fgetl(fid);              % Read the first line from the file
      while ~isequal(nextLine, -1)        % Loop while not at the end of the file
        lineArray{lineIndex} = nextLine;  % Add the line to the cell array
        lineIndex = lineIndex+1;          % Increment the line index
        nextLine = fgetl(fid);            % Read the next line from the file
      end
      fclose(fid);                        % Close the file
    
      lineArray = lineArray(1:lineIndex-1);              % Remove empty cells, if needed
      for iLine = 1:lineIndex-1                          % Loop over lines
        lineData = textscan(lineArray{iLine}, '%s', ...  % Read strings
                            'Delimiter', delimiter);
        lineData = lineData{1};                          % Remove cell encapsulation
        if strcmp(lineArray{iLine}(end), delimiter)      % Account for when the line
          lineData{end+1} = '';                          %   ends with a delimiter
        end
        lineArray(iLine, 1:numel(lineData)) = lineData;  % Overwrite line data
      end
    
    end
    

    Running this function on the sample file content from the question gives this result:

    >> data = read_mixed_csv('myfile.csv', ';')
    
    data = 
    
      Columns 1 through 7
    
        '04'    'abc'    'def'    'ghj'    'klm'    ''            ''        
        ''      ''       ''       ''       ''       'Test'        'text'    
        ''      ''       ''       ''       ''       'asdfhsdf'    'dsafdsag'
    
      Columns 8 through 10
    
        ''          ''    ''
        '0xFF'      ''    ''
        '0x0F0F'    ''    ''
    

    The result is a 3-by-10 cell array with one field per cell where missing fields are represented by the empty string ''. Now you can access each cell or a combination of cells to format them as you like. For example, if you wanted to change the fields in the first column from strings to integer values, you could use the function str2double as follows:

    >> data(:, 1) = cellfun(@(s) {str2double(s)}, data(:, 1))
    
    data = 
    
      Columns 1 through 7
    
        [  4]    'abc'    'def'    'ghj'    'klm'    ''            ''        
        [NaN]    ''       ''       ''       ''       'Test'        'text'    
        [NaN]    ''       ''       ''       ''       'asdfhsdf'    'dsafdsag'
    
      Columns 8 through 10
    
        ''          ''    ''
        '0xFF'      ''    ''
        '0x0F0F'    ''    ''
    

    Note that the empty fields results in NaN values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.