如何将AccountingForm应用于TableView而不影响行/列标签?(how to apply AccountingForm to TableView without affecting t

编程入门 行业动态 更新时间:2024-10-15 20:18:35
如何将AccountingForm应用于TableView而不影响行/列标签?(how to apply AccountingForm to TableView without affecting the row/column labels?)

我使用的是M 8.0.4。

我在演示Manipulate []中使用TableView来显示最终的解决方案数据,这是一个数字数据矩阵。

TableView非常好用,因为它带有一个自动滚动条来向下移动行,并在列的右边移动,这样就可以看到整个矩阵,同时保持整个显示区域的固定。 (我有一个图像在底部)

我无法找到解决方案的问题是,我需要格式化矩阵数据,以便它看起来不错。 否则,如果一个数据元素太大而无法容纳在单元格中,它将会环绕,从而使TableView不对齐。 我还需要将小数点调整为其他格式。

我无法将NumberForm和AccountForm应用于数据,然后将TableView应用于结果,因为TableView不喜欢查看这些包装器,它需要这些编号。

但是,如果将AccountingForm应用于TableView,则表示由TableView自动添加的行号和列号的数字也会被格式化。 我也不希望那些格式化。 行号应保持为整数,而不是浮点。 我只想将数据本身格式化。

我无法确定如何格式化Table视图内的数据。 当我使用FullForm来查看TableView中数据的保存方式时,我无法确定如何在不破坏TableView的情况下对其进行格式化。

我会显示问题,然后显示我的尝试。

(* make up some data, and make a TableView of it *) data = {{-1234.8, 0.123}, {0.12345678, 0.1234}} tbl = TableView[data, ItemSize -> {{3, {8}}}, DefaultBaseStyle -> 11, Spacings -> {.2, .1}, FrameStyle -> Gray]

请注意,行号和列号(标记)是正整数。

现在,我想格式化数据本身(下面使用的确切格式选项只是一个示例)

AccountingForm[tbl, {6, 3}, NumberSigns -> {"-", "+"}, NumberPadding -> {"", ""}, SignPadding -> True]

但现在Table行和列也被格式化:

TableView的FullForm是:

In[156]:= FullForm[tbl] TableView[List[List[-1234.8`,0.123`],List[0.12345678`,0.1234`]], Rule[ItemSize,List[List[3,List[8]]]], Rule[DefaultBaseStyle,11],Rule[Spacings,List[0.2`,0.1`]], Rule[FrameStyle,GrayLevel[0.5`]]]

所以TableView中的数据可以被使用

In[166]:= tbl[[1]] Out[166]= {{-1234.8,0.123},{0.12345678,0.1234}}

但是,当我使用ReplacePart[]将tbl[[1]]更改为AccountingForm版本的数据时, TableView不再有效:

formattedData = AccountingForm[data, {6, 3}, NumberSigns -> {"-", "+"}, NumberPadding -> {"", "0"}, SignPadding -> True]; In[245]:= tbl=ReplacePart[tbl,1->formatted] Out[245]= TableView[{{-1234.8,+0.123},{+0.123,+0.123}}, ItemSize->{{3,{8}}},DefaultBaseStyle->11,Spacings->{0.2,0.1}, FrameStyle->GrayLevel[0.5]]

所以,我打破了TableView。 因为它不再显示。

问题是:如何格式化进入TableView的数值数据而不影响行/列索引值?

Fyi,这就是TableView在一个Manipulate演示中的样子。 请注意自动滚动条。 (在这里,我没有使用NumberForm和它的朋友来进行格式化,但是我做的并不是很有效,如果可以的话,我宁愿使用NumberForm ,因此我的问题是)

谢谢

更新12/22/11 1:30 AM

下面是一个完整的代码示例,供Mike在下面回答他的回答

data = {{-1234.8, 0.123}, {0.12345678, 0.1234}}; newData = Map[AccountingForm[#, {6, 3}, NumberSigns -> {"-", "+"}, NumberPadding -> {"", ""}, SignPadding -> True] &, data, {2}]; tbl = TableView[newData, ItemSize -> {{3, {8}}}, Spacings -> {.2, .1}, FrameStyle -> Gray]

现在我如何使用上述的Cell命令? (这是在Manipulate中,而不是笔记本会话,Manipulate在一个单元格中运行,我不能在这个代码中创建单独的单元格,但是为了尝试它,我可以在一个新的笔记本中,但是将使用的实际代码这个解决方案必须在没有单元的情况下运行。

11月22日上午5点更新

我注意到一些不好的TableView性能。 考虑这段代码

Remove["Global`*"]; ClearSystemCache[] m1 = MemoryInUse[$FrontEnd]; N[m1/10^6] n = 256; data = Table[RandomReal[], {n}, {n}]; TableView[data, ContentSize -> {300, 300}] m2 = MemoryInUse[$FrontEnd] - m1; N[m2/10^6]

数据本身假设为实数的两倍,只有大约一半MB。 由于其他簿记数据结构,ByteCount说2 MB。

In[114]:= ByteCount[data]/10^6//N Out[114]= 2.10948

但是前端似乎使用了更多的RAM(对于整个TableView我的意思不仅仅是数据),有时我得到20 MB,有时甚至更多(一次得到100 MB)。 但是如果你在计算机上尝试了上述内容,你会注意到M在这方面遇到困难。 我认为这可能是表格部分的渲染导致M花费很多时间。

我不认为256乘256就是这么大的矩阵。 即使128乘128,它也很难在屏幕上显示它。 一旦启动,它就会很快并且没有问题。

它看起来像TableView尚未优化。 我想我只会用它来显示解决方案矩阵的一小部分,因为在演示中使用它来显示整个解决方案的性能不好,它会使演示看起来很糟糕。

问题是我的解决方案矩阵可能很大,我想要在有限的GUI空间中显示数据。 TableView是我所能找到的内置滚动条的东西。 Manipulate控件中没有任何其他东西可以使用,所以我必须使用TableView。

11/22/11下午4点更新

感谢Mike下面的提示,我现在正在研究使用Pane和Scollbars,它比TableView工作得更好。 (对于大数据仍然很慢,但不如TableView那么糟糕)

这是一个代码示例:

n = 32; data = Table[RandomReal[], {n}, {n}]; grid = Grid[data, Spacings -> {.4, .4}, Alignment -> Left, Frame -> All]; grid = Style[NumberForm[grid, {6, 5}], LineBreakWithin -> False]; pane = Pane[grid, ImageSize -> {200}, Scrollbars -> True]

到目前为止我唯一注意到的问题是,当n变大时,Pane会生成这个外框,表示生成了非常大的输出。 但是我确信有一种编程方式(选择某处)可以消除这种影响,我只是开始考虑这个问题,一旦有了我的咖啡,就应该能够找到解决办法。 (手指交叉)

好消息

只有在笔记本界面中才会显示“非常大的输出”。 当我把代码放入一个Manipulate并运行它时,它没有显示这个输出框。 所以,我现在很高兴,这个问题已经结束。 我从现在开始使用Pane。 (再次感谢迈克)

顺便说一下,要删除有问题的邮件,我发现这是一个有用的链接:

http://forums.wolfram.com/mathgroup/archive/2009/Apr/msg00935.html

但我不需要为我正在做的事情做任何事情。

I am using M 8.0.4.

I am use TableView in demo Manipulate[] to display final solution data, which is a matrix of numerical data.

TableView is very nice to use, since it comes with an automatic scroll bars to move down the rows and to the right over the columns, so that one can see the whole matrix, while keeping the overall display area fixed. (I have an image at the bottom)

The problem that I am not able to find a solution for, is that I need to format the matrix data, so that it looks nice. Otherwise, if the a data element is too large to fit in a cell, it will wrap around, making the TableView misaligned. I also need to adjust decimal point an such other formatting.

I can't apply NumberForm nor AccountForm to the data, and then apply TableView on the result, because TableView does not like to see those wrappers, it needs the numbers.

But if apply the AccountingForm to the TableView, then the numbers that represent the row numbers and column numbers, which are added by TableView automatically, get formatted as well. And I do not want those formatted as well. A row number should remain an integer, not floating point. I just want the data itself formatted.

I could not figure how to format the data from inside Table view. When I use FullForm to see how the data is kept inside TableView, I could not figure how to format it without breaking TableView.

I'll show the problem, then show what I tried.

(* make up some data, and make a TableView of it *) data = {{-1234.8, 0.123}, {0.12345678, 0.1234}} tbl = TableView[data, ItemSize -> {{3, {8}}}, DefaultBaseStyle -> 11, Spacings -> {.2, .1}, FrameStyle -> Gray]

Notice the row numbers and columns number (marked) are positive integers.

Now, I wanted to format the data itself, (the exact formatting options used below is just an example)

AccountingForm[tbl, {6, 3}, NumberSigns -> {"-", "+"}, NumberPadding -> {"", ""}, SignPadding -> True]

But now the Table rows and columns are also formatted:

The FullForm of TableView is:

In[156]:= FullForm[tbl] TableView[List[List[-1234.8`,0.123`],List[0.12345678`,0.1234`]], Rule[ItemSize,List[List[3,List[8]]]], Rule[DefaultBaseStyle,11],Rule[Spacings,List[0.2`,0.1`]], Rule[FrameStyle,GrayLevel[0.5`]]]

So the data in TableView can be pulled out using

In[166]:= tbl[[1]] Out[166]= {{-1234.8,0.123},{0.12345678,0.1234}}

But when I change tbl[[1]], using ReplacePart[], with an AccountingForm version of the data, TableView no longer works:

formattedData = AccountingForm[data, {6, 3}, NumberSigns -> {"-", "+"}, NumberPadding -> {"", "0"}, SignPadding -> True]; In[245]:= tbl=ReplacePart[tbl,1->formatted] Out[245]= TableView[{{-1234.8,+0.123},{+0.123,+0.123}}, ItemSize->{{3,{8}}},DefaultBaseStyle->11,Spacings->{0.2,0.1}, FrameStyle->GrayLevel[0.5]]

So, I broke TableView. Since it does not display any more.

Question is: How to format numeric data that goes into TableView without affecting the row/column index values?

Fyi, this is how the TableView looks inside one Manipulate demo I have. Notice the automatics scroll-bars. (In this one, I am not using NumberForm and its friends to do the formatting. But what I did is not very efficient, I'd rather use NumberForm if I can, hence my question)

thanks

update 12/22/11 1:30 AM

Here is a complete code example for Mike to follow up on his answer below

data = {{-1234.8, 0.123}, {0.12345678, 0.1234}}; newData = Map[AccountingForm[#, {6, 3}, NumberSigns -> {"-", "+"}, NumberPadding -> {"", ""}, SignPadding -> True] &, data, {2}]; tbl = TableView[newData, ItemSize -> {{3, {8}}}, Spacings -> {.2, .1}, FrameStyle -> Gray]

Now how exactly do I use the Cell command for the above? (This is in Manipulate, not a notebook session, Manipulate runs all in one cell. I can't make separate cells and such in this code. But for trying it, I can in a new notebook, but the actual code that will use this solution, has to run in a Manipulate with no Cells.

Update 12/22/11 5 am

I am noticing some not good performance of TableView. Consider this code

Remove["Global`*"]; ClearSystemCache[] m1 = MemoryInUse[$FrontEnd]; N[m1/10^6] n = 256; data = Table[RandomReal[], {n}, {n}]; TableView[data, ContentSize -> {300, 300}] m2 = MemoryInUse[$FrontEnd] - m1; N[m2/10^6]

The data itself, assuming double for the reals, is about half MB only. ByteCount says 2 MB due to other bookkeeping data struct.

In[114]:= ByteCount[data]/10^6//N Out[114]= 2.10948

But Front end seems to use much more RAM (for the whole TableView I mean not just the data), sometimes I get 20 MB and sometimes much more (got 100 MB at one time). But if you try the above on your computer, you'll notice M is having hard time with this. I think it might be the rendering of the table part that causes M to take so much time.

I do not think 256 by 256 is such a large matrix. Even with 128 by 128, it was having hard time to render it on the screen. Once it is up, then it is fast and no problem using it.

It looks like TableView is not yet optimized well. I think I will only use it to display small part of the solution Matrix as the performance is not good to use it in a demo to display the whole solution, it will make the demo look bad.

The problem is that my solution matrix can be large, and I wanted a away to display the data in limited amount of GUI space. TableView is all what I can find that comes with scrollbars build-in. Nothing else in the Manipulate controls has something like this to use, so I have to use TableView.

Update 12/22/11 4 PM

Thanks for the hint by Mike below, I am now looking at using Pane with Scollbars, and it is working better than TableView. (still little slow with large data, but not as bad as TableView)

Here is a code example of doing that:

n = 32; data = Table[RandomReal[], {n}, {n}]; grid = Grid[data, Spacings -> {.4, .4}, Alignment -> Left, Frame -> All]; grid = Style[NumberForm[grid, {6, 5}], LineBreakWithin -> False]; pane = Pane[grid, ImageSize -> {200}, Scrollbars -> True]

Only problem I noticed so far is that when n gets large, Pane generates this outer frame saying very large output was generated. But I am sure there is a programmatic way (option somewhere) to turn that effect off, I just started looking at this and should be able to find a way around this once I have my coffee. (fingers crossed)

good news

The "Very large output was generated" ONLY showed up when in notebook interface. When I put the code in a Manipulate and and run it, it did not show this output frame. So, I am happy now and this issue is closed. I'll use Pane from now on. (Thanks again to Mike)

btw, to remove the message in question, here is a useful link I found:

http://forums.wolfram.com/mathgroup/archive/2009/Apr/msg00935.html

but I did not need to do anything for what I am doing.

最满意答案

我想这是另一种不同的方法,可以作为另一个答案发布,尽管它明显借鉴了迈克的答案。

f = ToString @ AccountingForm[#, {6, 3}, NumberSigns -> {"-", "+"}, NumberPadding -> {"", ""}, SignPadding -> True] &; data = {{-1234.8, 0.123}, {0.12345678, 0.1234}}; TableView[Map[f, data, {2}], ItemSize -> {{3, {8}}}, DefaultBaseStyle -> 11, Spacings -> {.2, .1}, FrameStyle -> Gray]

Mathematica图形



由于使用ToString不直观,因此我决定再探索一下。 看起来, TableView抑制了字符串中的引号,但仅包含裸字符串。 例如:

string = "\"\"\"test\""; TableView[{{ string }}]

Mathematica图形

TableView[{{ {string} }}]

Mathematica图形

其他有趣的行为可以在2D格式的字符串中看到:

string = "\!\(\*SuperscriptBox[SqrtBox[\"5\"], \"2\"]\)"; TableView[{{ string }}]

Mathematica图形

TableView[{{ {string} }}]

Mathematica图形

I guess this is a different enough method to post as another answer, though it clearly borrows from Mike's answer.

f = ToString @ AccountingForm[#, {6, 3}, NumberSigns -> {"-", "+"}, NumberPadding -> {"", ""}, SignPadding -> True] &; data = {{-1234.8, 0.123}, {0.12345678, 0.1234}}; TableView[Map[f, data, {2}], ItemSize -> {{3, {8}}}, DefaultBaseStyle -> 11, Spacings -> {.2, .1}, FrameStyle -> Gray]

Mathematica graphics



Since using ToString was non-intuitive, I decided to explore it a bit more. It seems that TableView suppresses quotation marks in strings, but only bare strings. e.g.:

string = "\"\"\"test\""; TableView[{{ string }}]

Mathematica graphics

TableView[{{ {string} }}]

Mathematica graphics

Other interesting behavior can be seen with 2D formatted strings:

string = "\!\(\*SuperscriptBox[SqrtBox[\"5\"], \"2\"]\)"; TableView[{{ string }}]

Mathematica graphics

TableView[{{ {string} }}]

Mathematica graphics

更多推荐

本文发布于:2023-08-01 07:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1354921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   应用于   如何将   标签   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!