Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String indexing in MATLAB: single vs. double quote

Tags:

string

matlab

I have a matrix of strings such as the following:

readFiles = [   
            "11221", "09";
            "11222", "13";
            "12821", "06";
            "13521", "02";
            "13522", "13";
            "13711", "05";
            "13921", "01";
            "14521", ".001";
            "15712", ".003"
            ];

These are used to access to some folders and files in an automatic way. Then what I want to do is the following (with ii being some integer):

FileName = strcat('../../Datasets/hc-1/d',readFiles(ii,1),'/d',...
                     readFiles(ii,1),readFiles(ii,2),'.dat');
data(ii,:) = LoadBinary(FileName, 6);

The string FileName is then generated using double quotes (I'm not sure why). So its value is:

FileName = 

"../../Datasets/hc-1/d13921/d1392101.dat"

The function LoadBinary() returns an error when trying to perform the following operation:

lastdot = strfind(FileName,'.');
FileBase = FileName(1:lastdot(end)-1); % This line 

However, if I create the string FileName manually using single quotes, the function works okay.

In a nutshell, if I try to index a string (FileName(1:lastdot(end)-1)) that is created with the lines above (leading to FileName = "../../Datasets/hc-1/d13921/d1392101.dat"), MATLAB returns an error. If I create it manually with single quotes (FileName = '../../Datasets/hc-1/d13921/d1392101.dat'), the function works right.

Why does this happen? Is there a way to fix it (i.e. convert the double-quoted string into a single-quoted one)?

like image 357
Tendero Avatar asked Apr 02 '18 21:04

Tendero


People also ask

What is the difference between single and double quoted string?

A single-quoted string does not have variables within it interpreted. A double-quoted string does. Also, a double-quoted string can contain apostrophes without backslashes, while a single-quoted string can contain unescaped quotation marks.

Do strings require single or double quotes?

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!

What is the difference between single and double quotation marks in coding?

As far as language syntax is concerned, there is no difference in single or double quoted string. Both representations can be used interchangeably. However, if either single or double quote is a part of the string itself, then the string must be placed in double or single quotes respectively.


1 Answers

Double quotes are String array, while Single one are Char array. You can convert your string array to a char one using the function char. So you'd write :

CharFileName = char(FileName)

And it should resolve your issue.

like image 134
Jean Rostan Avatar answered Oct 10 '22 20:10

Jean Rostan