简体   繁体   English

MATLAB中的字符串索引:单引号与双引号

[英]String indexing in MATLAB: single vs. double quote

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): 然后我想要做的是以下( ii是一些整数):

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). 然后使用双引号生成字符串FileName (我不知道为什么)。 So its value is: 所以它的价值是:

FileName = 

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

The function LoadBinary() returns an error when trying to perform the following operation: 尝试执行以下操作时,函数LoadBinary()返回错误:

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. 但是,如果我使用单引号手动创建字符串FileName ,则该函数可以正常工作。

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. 简而言之,如果我尝试索引使用上面的行创建的字符串( FileName(1:lastdot(end)-1) )(导致FileName = "../../Datasets/hc-1/d13921/d1392101.dat"返回错误。 If I create it manually with single quotes ( FileName = '../../Datasets/hc-1/d13921/d1392101.dat' ), the function works right. 如果我用单引号手动创建它( FileName = '../../Datasets/hc-1/d13921/d1392101.dat' ),该功能正常。

Why does this happen? 为什么会这样? Is there a way to fix it (ie convert the double-quoted string into a single-quoted one)? 有没有办法解决它(即将双引号字符串转换为单引号字符串)?

Double quotes are String array, while Single one are Char array. 双引号是String数组,而Single是Char数组。 You can convert your string array to a char one using the function char . 您可以使用函数char将字符串数组转换为char数组。 So you'd write : 所以你写道:

CharFileName = char(FileName)

And it should resolve your issue. 它应该解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM