現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Convert a multidimensional array into respective single array
7 ビュー (過去 30 日間)
古いコメントを表示
Willam Willam
2012 年 12 月 29 日
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 件のコメント
Image Analyst
2012 年 12 月 29 日
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
2012 年 12 月 30 日
Sir, I used fread() function previously. Is that this function cant used?
Willam Willam
2012 年 12 月 30 日
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].
回答 (2 件)
Walter Roberson
2012 年 12 月 30 日
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 件のコメント
Willam Willam
2012 年 12 月 30 日
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).
Image Analyst
2012 年 12 月 30 日
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
2012 年 12 月 30 日
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
2012 年 12 月 30 日
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]
Willam Willam
2012 年 12 月 30 日
Sir Image Analyst, my purpose of doing this is I want embed those data into my grayscale image.
Walter Roberson
2012 年 12 月 30 日
Change
ascii_binary = ascii_binary +0;
to
ascii_binary = ascii_binary - '0';
Willam Willam
2012 年 12 月 30 日
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)
Image Analyst
2012 年 12 月 30 日
If it's text, you need to convert it to a number.
num8bit = uint8(str2num(readText));
Walter Roberson
2012 年 12 月 30 日
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
2012 年 12 月 30 日
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
2012 年 12 月 30 日
%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
Willam Willam
2012 年 12 月 30 日
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));
Image Analyst
2012 年 12 月 30 日
You didn't use char() like I said. Please click the "show older comments" link above and see Walter's comment.
Willam Willam
2012 年 12 月 30 日
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
Walter Roberson
2012 年 12 月 30 日
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
2012 年 12 月 31 日
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?
Walter Roberson
2012 年 12 月 31 日
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
2012 年 12 月 31 日
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?
Walter Roberson
2012 年 12 月 31 日
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
2013 年 1 月 1 日
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
Walter Roberson
2013 年 1 月 1 日
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
2013 年 1 月 1 日
Sir Walter, got sample code that you suggested that can solve this problem? Thanks.
Willam Willam
2013 年 1 月 1 日
Then is that the embed variable will be indexed? I mean will be like A(1)?
Willam Willam
2013 年 1 月 1 日
Ok. I will try it when I'm free. Because prepare for final exam. Thanks ya Sir Walter
Willam Willam
2013 年 1 月 9 日
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
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
