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

  • Home
  • SEARCH
  • 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 8303713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:36:30+00:00 2026-06-08T17:36:30+00:00

From the beginning. I have data in a csv file like: La Loi des

  • 0

From the beginning.

I have data in a csv file like:

La Loi des rues,/m/0gw3lmk,/m/0gw1pvm
L'Étudiante,/m/0j9vjq5,/m/0h6hft_
The Kid From Borneo,/m/04lrdnn,/m/04lrdnt,/m/04lrdn5,/m/04lrdnh,/m/04lrdnb

etc.

This is in UTF-8 format. I import this file as follows (taken from somewhere else):

feature('DefaultCharacterSet','UTF-8');
fid = fopen(filename,'rt');         %# 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

This makes an array with the UTF-8 text within it. {3×1} array:

'La Loi des rues,/m/0gw3lmk,/m/0gw1pvm'
'L''Étudiante,/m/0j9vjq5,/m/0h6hft_'
'The Kid From Borneo,/m/04lrdnn,/m/04lrdnt,/m/04lrdn5,/m/04lrdnh,/m/04lrdnb'

Now the next part separates each value into an array:

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',',');
    lineData = lineData{1};                        %# Remove cell encapsulation
    if strcmp(lineArray{iLine}(end),',')           %# Account for when the line
      lineData{end+1} = '';                        %# ends with a delimiter
    end
    lineArray(iLine,1:numel(lineData)) = lineData; %# Overwrite line data
  end

This outputs:

'La Loi des rues'   '/m/0gw3lmk'    '/m/0gw1pvm'    []  []  []
'L''�tudiante'  '/m/0j9vjq5'    '/m/0h6hft_'    []  []  []
'The Kid From    Borneo'    '/m/04lrdnn'    '/m/04lrdnt'    '/m/04lrdn5'    '/m/04lrdnh'    '/m/04lrdnb'

The problem is that the UTF-8 encoding is lost on the textscan (note the question mark I now get whereas it was fine in the previous array).

Question: How do I maintain the UTF-8 coding when it translates the {3×1} array into a 3xN array.

I can’t find anything on how to keep UTF-8 encoding in a textscan of an array already in the workspace. Everything is to do with importing a text file which I have no problems with – it is the second step.

Thanks!

  • 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-06-08T17:36:32+00:00Added an answer on June 8, 2026 at 5:36 pm

    Try the following code:

    %# read whole file as a UTF-8 string
    fid = fopen('utf8.csv', 'rb');
    b = fread(fid, '*uint8')';
    str = native2unicode(b, 'UTF-8');
    fclose(fid);
    
    %# split into lines
    lines = textscan(str, '%s', 'Delimiter','', 'Whitespace','\n');
    lines = lines{1};
    
    %# split each line into values
    C = cell(numel(lines),6);
    for i=1:numel(lines)
        vals = textscan(lines{i}, '%s', 'Delimiter',',');
        vals = vals{1};
        C(i,1:numel(vals)) = vals;
    end
    

    The result:

    >> C
    C = 
        'La Loi des rues'        '/m/0gw3lmk'    '/m/0gw1pvm'              []              []              []
        'L'Étudiante'            '/m/0j9vjq5'    '/m/0h6hft_'              []              []              []
        'The Kid From Borneo'    '/m/04lrdnn'    '/m/04lrdnt'    '/m/04lrdn5'    '/m/04lrdnh'    '/m/04lrdnb'
    

    Note that when I tested this, I encoded the input CSV file as “UTF-8 without BOM” (I was using Notepad++ as editor)

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

Sidebar

Related Questions

I have a web form used for importing data from a CSV file. It
I have the below script to import data from a csv file on my
I'm supposed to add data from my csv file into my database. I have
I am using PHP to expose vehicle GPS data from a CSV file. This
We have written a number of SSIS packages that import data from CSV files
I am trying to create a CSV file from a dynamically generated table.I have
I have recently started studying about threads. I thought of starting from the beginning
In a file I am skipping number of characters from beginning of file InputStreamReader
with-open-file will read from the beginning of a file. If the file is VERY
I would like to replace each two spaces from the beginning of each line,

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.