Convert a multidimensional array into respective single array
Show older comments
Hi. I want ask how I need to convert the data that I read from text file and put it in the single the form of 8 bits per each array. Thanks for help. Appreciate.
4 Comments
Image Analyst
on 29 Dec 2012
Is the data already read from the file, or do you need code for that too? Have you tried dlmread(), csvread(), importdata(), or textscan()?
Willam Willam
on 30 Dec 2012
Walter Roberson
on 30 Dec 2012
fread() is for binary files, not for converting text.
Willam Willam
on 30 Dec 2012
Answers (2)
Image Analyst
on 29 Dec 2012
Is this what you mean:
array8bit = uint8(doubleArray);
Walter Roberson
on 30 Dec 2012
Putting your data into different arrays is not advised. Please read http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
To convert your values, use
bin2dec(A,8)
This will return a character array, one row for each input value, with each character being '0' or '1'. These will not be the numbers 0 and 1. If you want the numbers 0 and 1 instead of the characters '0' and '1', then use
bin2dec(A,8) - '0'
The result would be a number of rows, each 8 columns wide, with one row per input number.
If you were hoping that (for example) the input value 5 would convert to the decimal number 00000101, then although the conversion to decimal-coded-binary could be done, keep in mind that decimal values automatically drop leading 0's on output, so such an entry would print out as 101 rather than 00000101.
27 Comments
Willam Willam
on 30 Dec 2012
Image Analyst
on 30 Dec 2012
I'm not sure what you mean. All variables are binary - they're in a computer after all. I can have an "A", a 65 decimal, a 41 hex, a 101 octal, or a 1000001 binary, or a string '1000001' - when you get down to it, they're all binary. In your file you have text, though the text can be ASCII numbers like a "3", but like I said, it's basically binary because if you looked at it bit for bit with the the right editor, that's how it would look on disk. Let's ask this: how do you plan on using this text after you read it in? Do you want the text to be unsigned 8 bit integer arrays of numbers?
Walter Roberson
on 30 Dec 2012
Could you give a sample line of your data?
Do you want each distinct output array to consist of exactly one character, with the next character being in a different array? If so then that really is not advised. It would also not be good to have each distinct output array consist of only 8 values that together represent 8 bits of input. Use one row per value instead of one matrix per value.
I am confused about whether you would want the decimal value 50 represented as [0 0 0 1 1 0 1 0] or as '00011010' or as an unsigned 8 bit value whose bits were 00011010 or as a single character '2' (which is character position #50) ?
Willam Willam
on 30 Dec 2012
Willam Willam
on 30 Dec 2012
Walter Roberson
on 30 Dec 2012
Change
ascii_binary = ascii_binary +0;
to
ascii_binary = ascii_binary - '0';
Willam Willam
on 30 Dec 2012
Image Analyst
on 30 Dec 2012
If it's text, you need to convert it to a number.
num8bit = uint8(str2num(readText));
Walter Roberson
on 30 Dec 2012
It isn't text, it is cell according to the error message.
William, please show
size(readText)
class(readText)
also, please show a sample input line from test.txt
Image Analyst
on 30 Dec 2012
Yep, we need that. Though, in advance, if it's just a one element cell, you can try to convert it from a cell to text with char():
readText = {'156'} % A single cell with a string inside it.
num8bit = uint8(str2num(char(readText)))
readText = {'123', '234'} % Cell array with 2 cells in it.
num8bit = uint8(str2num(char(readText)))
but let us know either way....
Willam Willam
on 30 Dec 2012
Willam Willam
on 30 Dec 2012
Image Analyst
on 30 Dec 2012
You didn't use char() like I said. Please click the "show older comments" link above and see Walter's comment.
Willam Willam
on 30 Dec 2012
Walter Roberson
on 30 Dec 2012
You are trying to embed the string '1w' ? If so then importdata() is not appropriate. Instead
readText = fileread('Test.txt');
char_in_bit = uint8(readText);
Willam Willam
on 31 Dec 2012
Walter Roberson
on 31 Dec 2012
Are you still using
uint8(str2num(char(readText)))
to find the size of the array? That is not likely to work. Just use length(char_in_bit) as the number of bytes you are going to embed.
But to confirm: the '1w' is to mean
00110001
01110111
and the fact that '1w' happens to start with a digit is not relevant to the process, right? The file could have easily had something like '!delete .' as the string to embed, right ?
Willam Willam
on 31 Dec 2012
Walter Roberson
on 31 Dec 2012
Are you still using
ascii_binary = ascii_binary +0;
that needs to be
ascii_binary = ascii_binary - '0';
Please show your current code and indicate which line you run into the problem on.
Willam Willam
on 1 Jan 2013
Walter Roberson
on 1 Jan 2013
Look at your code block,
for i=1:size_char
for j=1:size_char-(size_char-1)
embed = bitget(ascii_binary, j);
embed = embed(i,:);
display(embed);
end
end
In each iteration of the inner loop, you use bitget() to extract a bit into "embed", and then you extract one row from "embed" and assign that one row to "embed", and then you display the value of "embed" -- and then in the next iteration of the loop, you overwrite everything you did. So at the end, "embed" is going to be whatever was assigned to it on the last iteration of the loop, with nothing else stored in it.
I would also think it very likely that you do not want to use bitget(), that the value you want to fetch is ascii_binary(i,j)
Willam Willam
on 1 Jan 2013
Walter Roberson
on 1 Jan 2013
embed = reshape(ascii_binary .', [], 1);
with no loop.
Willam Willam
on 1 Jan 2013
Walter Roberson
on 1 Jan 2013
Yes.
Willam Willam
on 1 Jan 2013
Willam Willam
on 9 Jan 2013
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!