Convert a multidimensional array into respective single array

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

Is the data already read from the file, or do you need code for that too? Have you tried dlmread(), csvread(), importdata(), or textscan()?
Sir, I used fread() function previously. Is that this function cant used?
fread() is for binary files, not for converting text.
From Image Analyst comment, I had use importdata() function and manage get my data in the form of A=[50 20]. But how I need to convert it into A=[11111111 11111111] and separate it into two arrays like A1=[11111111] A2=[11111111].

Sign in to comment.

Answers (2)

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

Sir Walter, I get what you meant. From your experience, which ways is suitable for my case? I mean is read the data from text, convert into binary and store it into respective array with each array has 8 bits of data(one character in ASCII).
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?
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) ?
readText = importdata('test.txt');
%fclose(fid); %close the file function
char_in_bit = uint8(readText);
display(char_in_bit);
ascii_binary = dec2bin(char_in_bit, 8);
ascii_binary = ascii_binary +0;
size_char = length(char_in_bit); %determine the length of the text file
length = length(ascii_binary);
display(size_char);
Sir Walter, I want my decimal value 50 represented in [0 0 0 1 1 0 1 0]
Sir Image Analyst, my purpose of doing this is I want embed those data into my grayscale image.
Change
ascii_binary = ascii_binary +0;
to
ascii_binary = ascii_binary - '0';
Sir Walter, this error has appeared. Error using uint8 Conversion to uint8 from cell is not possible.
Error in test (line 48)
char_in_bit = uint8(readText)
If it's text, you need to convert it to a number.
num8bit = uint8(str2num(readText));
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
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....
%fid = fopen('test.txt'); %open the text file
readText = importdata('test.txt');
display(size(readText));
display(class(readText));
%fclose(fid); %close the file functionzz
char_in_bit = uint8(str2num(readText));
display(char_in_bit);
ascii_binary = dec2bin(char_in_bit, 8);
ascii_binary = ascii_binary +0;
%size_char = length(char_in_bit); %determine the length of the text file
length = length(ascii_binary);
%display(size_char);
display(BlockRONI);
%display(class(ascii_binary));
this is my input that stored in test.txt --> 1w
Sir Image Analyst, still got this error. Error using str2num (line 33) Requires string or character array input.
Error in test (line 50) char_in_bit = uint8(str2num(readText));
You didn't use char() like I said. Please click the "show older comments" link above and see Walter's comment.
Oh ya. I had used the uint8(str2num(char(readText))), it can get the size of the matrix, but cant show the bits. ans =
1 1
char
char_in_bit =
[]
BlockRONI =
625
embed =
Empty matrix: 0-by-8
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);
Oh ya. Can already Sir Walter. But the array not indexed. While embedding process start, it always took the last array embed into the image. Did Sir Walter what is happening?
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 ?
Now the embedding and extracting is successful already. Like I got 2 arrays A=[1 1 1 1 1 1 1 1] and A=[1 0 1 1 0 0 0 1]. While embedding start, it tends to take the last array A=[1 0 1 1 0 0 0 1]. It can't start embed with A=[1 1 1 1 1 1 1 1] then follow by A=[1 0 1 1 0 0 0 1]. Is that indexing the array problem or Matlab itself treat the array like this?
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.
fid = fopen('test.txt'); %open the text file
readText = fread(fid);
%readText = char(readText);
fclose(fid); %close the file functionzz
char_in_bit = char(readText);
%display(char_in_bit);
ascii_binary = dec2bin(char_in_bit,8);
ascii_binary = ascii_binary-'0';
display(ascii_binary);
size_char = length(char_in_bit); %determine the length of the text file
length = length(ascii_binary);
display(size_char);
display(length);
display(BlockRONI);
embed = [];%convert ascii_binary into array form
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
%this part start got problem
for a=1
if blocksize == 2
n=1;
embed(a);
for r=1:2
for c=1:2
block(r,c)=bitset(block(r,c),1,embed(n));
%display(block);
n=n+1;
%pause;
block(r,c)=bitset(block(r,c),2,embed(n));
%display(block);
n=n+1;
%pause;
end
end
if (x2_RONI+blocksize) > (N_RONI+startx-1)
if y2_RONI+blocksize < (M_RONI+starty)
x2_RONI=startx;
y2_RONI=y2_RONI+blocksize;
end
else
x2_RONI=x2_RONI+blocksize;
end
end
end
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)
Sir Walter, got sample code that you suggested that can solve this problem? Thanks.
embed = reshape(ascii_binary .', [], 1);
with no loop.
Then is that the embed variable will be indexed? I mean will be like A(1)?
Ok. I will try it when I'm free. Because prepare for final exam. Thanks ya Sir Walter
Sir walter, i tested already. It works but only for 1st array only. How to make it all arrays are recorded through this code? Thanks

Sign in to comment.

Products

Tags

Asked:

on 29 Dec 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!