0

I have an array of bytes (which represents e.g. a CSV file) which are coming from C# in Matlab and I want to load this now without creating an temporary file.

Is there a possibility to do that? All methods I found are only taking filenames or requires that they will write it into a file.

Thanks!

3
  • Expect that on the server where it will run we don't have any rights to create files.. Commented Mar 27, 2018 at 10:02
  • You have an array of bytes, but where? Internal memory? Is it a variable already in matlab? If it is in matlab, you could use textscan to convert a csv-like character vector to individual values. Commented Mar 27, 2018 at 11:14
  • Yes, it is a variable with uint8.. Commented Mar 27, 2018 at 11:28

1 Answer 1

3

Here I made a small csv file consisting of 8 values. Read it as uint8 it in matlab and used textscan to retrieve the original 8 values.

orgdat =    [132   231   334   234;   132   432   423   334];
csvwrite('temp.csv',orgdat);
fid = fopen('temp.csv');
dat = fread(fid,inf,'*uint8');fclose(fid);
cell2mat(textscan(char(dat),'%f,%f,%f,%f'))
>>
ans =
   132   231   334   234
   132   432   423   334

Or if you just want to scan for numbers:

result = cell2mat(textscan(char(dat),'%f','Delimiter',','))
>>
result =

   132   231   334   234   132   432   423   334

To reshape it you could look for the number of rows and reshape

rows = sum(dat==10)+(dat(end)~=10);
reshape(result,[],rows)'
Sign up to request clarification or add additional context in comments.

6 Comments

That's exactly what I don't want. This is creating a file! And I don't have the data in clear text, I have them in bytes[]
That's why it is an example. I create a variable called dat that has bytes[] in it (32 bytes to be exact). Then in one line I convert it back to the original values. So I read a csv that is encoded as bytes[] variable. I thought that is what you wanted, if not, please make your question clear with an example. stackoverflow.com/help/how-to-ask
sorry I went a little bit to fast over your example, brain didn't worked that well after lunch :D I just have tow more question, 1. you are using '%f%f%f%f' because we have 4 columns in one row, correct? What do I do if I don't know about the columns size? 2. What if it contains characters e.g. 1.64E-07?
correct. Basically you are sorting on commas (byte value 44) and returns (byte value 10). What you could do is split the byte[] based on value 10, and then count how many 44's you have. That +1 is the nr of columns. Scientific notation is no problem for textscan. Try cell2mat(textscan('1.64E-07','%f')). It will return the right number.
Yeah. Updated the answer. It had 1-3-5-7 ; 2-4-6-8 as the order. So it took column first. You want row first, since that's how you read it in.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.