MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by ZK on 10 Jan 2013

Hi, I have another question. I have a big number something like 200308091234562. I would like to appeal to indicated parts. To other calculations.

So I would like to have something like this from this big number.

A = 2003 B = 08 C = 09 D = 123

Which funtion should I use. Thank You.

*No products are associated with this question.*

Answer by Andreas Goser on 10 Jan 2013

Edited by Andreas Goser on 10 Jan 2013

I suggest treating this numerical value as a string ('chararcter array') and us such operations, e.g.

s='200308091234562' A=s(1:4)

Maybe you want to do math with it afterwards:

str2num(A)*2

Answer by Jan Simon on 10 Jan 2013

If 200308091234562 is a double:

d = 200308091234562 A = floor(d / 1e11); B = rem(floor(d / 1e9), 100); C = rem(floor(d / 1e7), 100); D = rem(floor(d / 1e4), 1000);

If the value is a string:

s = '200308091234562' A = s(1:4); B = s(5:6); C = s(7:8); D = s(9:11);

ZK on 10 Jan 2013

Thank You this is it. But when textread readed it like a 1x1 cell. I should convert it to double or string?

Jan Simon on 10 Jan 2013

Dear ZK, I've presented two different solutions. Therefore "this is it" does not reveal in which format the data are available. Instead of clarifying this, you ask the next question, whose answer require the knowledge of the actual format again. "textread read it as 1x1 cell" is a vague description again, which requires to guess the details.

I suggest the following pattern:

My file contains lines like: 200308091234562 200308099812348 200308012803753 I read it with: data = textread(fid, '%s'); Now I get a {1x1} cell, which contains a cell string: data = {{'200308091234562', '200308099812348', '200308012803753'}} I want to get: A = 2003; % or '2003' ? B = 8; % or '08' ? C = 9; D = 123;

Then it would be clear, what "I have a big number" means and what you want. Ok?

ZK on 10 Jan 2013

OK. Jan Simon, thank You for answer and patience. I will express more precisely my problems in future.

Your suggested pattern presents my problem, with one difference I used textread to read by one line with headerlines.

I would like convert {1x1} cell to string because for this method reading numbers is most clear for me. Can You present me a good formula for it?

## 5 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/58413#comment_121813

What does "I have a number" exactly mean? Is this a string or a VPI number? Is this written to a file or a vector of UINT8 values between 0 and 9?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/58413#comment_121836

So, this is only a number loaded from database.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/58413#comment_121838

Sorry, ZK, this does still not explain which type it is. Is it a double? Then what does the leading zero of "B = 08" mean? Please do not let us guess the details.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/58413#comment_121841

It is loaded by textread from document. Leading zero means that this is August.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/58413#comment_121891

@ZK: Please post the type of the variable, which contains the shown value. Use the Matlab command

class()to find out the type in case of doubt. Numbers do not have a leading zero and in '08' it is surely not the '0' which indicates that it is August, but the '8'.