Matlab Error: Conversion to double from cell is not possible.
古いコメントを表示
NOTE: No other resolutions in Matlab forums have been helpful yet.
Please assist to address conversion from double to cell issue. Thank you in advance.
% > PURPOSE:
% 1. Save two characters from var1 (starting from right-side to left-side)
% 2. Result MUST SAVE as a SINGLE string, so that it could be saved
% in 1 Box/Block of Excel worksheet (.xlsx)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
clc
var1 = 'ab0af00960'
outMat = zeros(2,5,2);
for i =1:2
lenVar1 = length(var1);
for j = 1:5
temp1 = {[var1(lenVar1 - 1) var1(lenVar1)]};
outMat(i,j,1) = temp1;
lenVar1 = lenVar1 - 2;
outMat(i,j,2) = i;
end
end
% > ERROR Received:
% Conversion to double from cell is not possible.
%
% Error in Conversion (line 17)
% outMat(i,j,1) = temp1;
回答 (1 件)
dpb
2015 年 11 月 19 日
There's no reason for cell array to begin with..
% 1. Save two characters from var1 (starting from right-side to left-side)
outMat=var1(end-1:end);
3 件のコメント
A D
2015 年 11 月 19 日
dpb
2015 年 11 月 19 日
zeros(2,5,2)
is double numeric array in which you're speaking of putting character data. Why would you propose doing that?
I just recognized you as the same poster with the query about writing numeric values to Excel to be interpreted as strings. If you'll recall from there I showed that in order for the xlswrite/Excel combo to recognize what it thinks is a numeric value in a string as both numeric and to place all the digits in a single cell you have to forcibly write it as a string value with the single quote ' prefix --
The last example and subsequent comment of the first Answer I provide there demonstrate for a numeric argument. For a string argument that contains what can be interpreted as numeric values you also have to force it to be interpreted as a string; otherwise Excel thinks its numeric and autoconverts. To write your substring as a string--
>> xlswrite('AD.xls',{sprintf('''%s',matOut)})
>> [num,txt]=xlsread('AD.xls')
num =
[]
txt =
'60'
>> delete 'ad.xls'
To reiterate the previous lesson, remove the cell brackets ...
>> xlswrite('AD.xls',sprintf('''%s',out))
>> [num,txt]=xlsread('AD.xls')
num =
6 0
txt =
'' '' ''
>>
"...solution still has size: 1*2; where as intention is to get size: 1*1"
Doesn't matter in the end if the objective is to put it into Excel as a string as noted above you'll still have to encapsulate it in a cell string and prefix it with the single-quote ' for Excel to not interpret it as numeric if send the character string as cell. It all just shows why one should avoid Excel... :)
ADDENDUM
But the reason for not using the cellstring as the return of the proposed function is that subsequent use above--*sprintf* and friends aren't implemented for cell inputs so if save it that way subsequently must turn around and apply char or equivalent to it to format it with the Excel character string prefix anyway. Therefore, seemed no point in doing so...
sprintf('%s\n%s',string2,string2)
Error using sprintf
Function is not defined for 'cell' inputs.
>>
カテゴリ
ヘルプ センター および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!