简体   繁体   English

如何将数字作为字符串写入Excel文件?

[英]How do I write a number as a string to an Excel file?

I have a cell array in MATLAB which contains numeric data stored as a string. 我在MATLAB中有一个单元格数组,其中包含存储为字符串的数字数据。 I would like to maintain this string format when I export the file. 我想在导出文件时保持这种字符串格式。 However, when I use xlswrite , Excel automatically converts these to double values. 但是,当我使用xlswrite ,Excel会自动将这些值转换为double值。 Does anyone know how to prevent this? 有谁知道如何防止这种情况?

Here's an example of what I have written: 这是我写的一个例子:

    C = {'123'};
    xlswrite('example.xls', C);

When I check the file, "123" is stored as a number. 检查文件时,“123”存储为数字。 Specifically, I can perform arithmetic operations on it. 具体来说,我可以对它进行算术运算。

Insert a single quotation in front of each number before writing to file. 在写入文件之前,在每个数字前面插入一个引号。 The single quotation at the beginning informs Excel that you wish to input a string regardless of its contents: 开头的单引号告知Excel您希望输入字符串而不管其内容如何:

C = {'''123'};
xlswrite('example.xls', C);

You need to use two ' characters to symbolize a single quote to ensure MATLAB doesn't ambiguously think that you're trying to start a string... so to be sure we're on the same page: 您需要使用两个'字符来表示单引号,以确保MATLAB不会模糊地认为您正在尝试启动字符串...所以要确保我们在同一页面上:

C =  {'''123');
      |||   |
      abb   a

a denotes the starting and ending of the string and b denotes the declaration of a single quotation. a表示字符串的开头和结尾, b表示单引号的声明。

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

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